In the previous chapter we learned how to run a block of commands more than once. That's why we introduced a
for loop and reviewed its main use cases. The purpose of this chapter is to improve our knowledge by solving some more complex problems with loops, used for exams. For some of them we will show examples of detailed solutions, and for others we will leave only guidance.
Before we start working, it will be good to review again the
for loop construction:
For loops consist of:
- Initialization block in which the variable-counter is declared (
var i) and its initial value is set.
- Repeat condition (
i <= 10), executing once, before each iteration of the loop.
- Restarting the counter (
i++) – this code is executed after each iteration.
- Body of the loop – contains random block of source code.
Let's solve a few problems with loops from exams in SoftUni: