Loops are used to repeatedly execute some logic.
The two most common types are the while loop
(indefinite looping) and the for loop
(definite, or counted looping).
There is also the for each
loop, that will come up in a later concept.
The for loop
consists of a header and a code block that contains the body of the loop wrapped in curly brackets.
The header consists of 3 components separated by semicolons ;
: init-statement, condition, and another expression.
Each of these may be empty.
for (init-statement; condition; expression) {
some_statement;
}
The while loop
executes its body as long as its condition check is true.
The code snippet below shows how to transform a for into a while loop.
init-statement;
while(condition) {
some_statement;
expression;
}
When working with loops is often required to add 1 or subtract 1 from a counter variable.
This is so common, that the operation has special operators: ++
and --
.
They come in a prefix and a postfix form. The prefix changes the variable before use in the statement and the postfix version afterward. You probably want the prefix version most of the time.
int a{3};
int b{--a};
// b is 2, a is now 2
int c{a++};
// c is 2, a is now 3
The init component usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the post component usually increments the counter at the end of each repetition.
int sum{0};
for (int i{1}; i < 10; ++i) {
sum += i;
}
This loop will sum the numbers from 1
to 9
(including 9
).
Inside a loop body, you can use the break
keyword to stop the execution of the loop entirely:
int sum{2};
while(true) {
sum *= 2;
if (sum > 1000)
break;
}
// sum is now 1024
In contrast, the keyword continue
only stops the execution of the current iteration and continues with the next one:
int equal_sum{0};
for (int i{1}; i < 7; ++i) {
if (n%2 == 1) {
continue;
}
equal_sum += i;
}
// equal_sum is now 12
Note: it is usually easier to understand the logic of the loop, when the use of
break
andcontinue
is minimized or entirely avoided.