Each loop statement in C++ has its loop body which is executed repeatedly until the loop condition expression that yields a loop condition becomes false
(otherwise, the loop will run “forever”). The statement continue
can be used to interrupt the current iteration and skip the rest of the loop body to immediately check the loop condition before continuing. A whole loop statement can be completely interrupted by using one or the another termination statements: break
, goto
, return
, throw
. The break
terminates the innermost enclosing loop statement (or switch
statement, see Switch Statement in Modern C++), while the rest statements can be used to terminate the loop of any nesting level, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { int i{5}; for (; i < 10; ++i) while (true) // the nested loop goto done; // interrupts the both loops /* * Prints "10" if `break`, "50" - if `goto` and nothing - if * `return` or `throw` were used to interrupt the nested loop. */ done: std::cout << i << "\n"; } |
do
and while
Loops
A do
-loop executes its body at least once because the expression that yields a loop condition is evaluated and checked after the each iteration, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string_view> #include <vector> void wish_well(int max = 0) { static const std::vector<std::string_view> wishes{ "I wish you happiness!", "Good luck!", "I wish you success!"}; do std::cout << wishes[max%wishes.size()] << "\n"; while (max-- > 0); } int main() { wish_well(); // wishes well anyway :-) wish_well(1); // wishes well 1 + 1 times! :-) } |
A while
-loop is similar to do
-loop except that the loop condition is evaluated and checked before the each iteration, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string_view> #include <vector> void wish_well(int max = 0) { static const std::vector<std::string_view> wishes{ "I wish you happiness!", "Good luck!", "I wish you success!"}; while (max-- > 0) std::cout << wishes[max%wishes.size()] << "\n"; } int main() { wish_well(); // never wishes well :-( wish_well(1); // wishes well 1 time! :-) } |
Please note, either do
-loop or while
-loop are preferable when the loop variable (or something that affects the loop condition) must be updated in the loop body. (Usually, the number of iterations is unknown beforehand in this case.)
for
Loop
The general for
-loop statement should be used instead of range-for-statement when a greater control over the loop is required. The syntax is follow:
1 |
for (init-expression; loop-condition-expression; update-expression) expression |
Any of the expressions above can be omitted, so the simplest form of the general for
-loop is for(;;);
(which just blocks the execution thread forever). The init-expression has to be used to declare variables into the scope of for
-loop statement, the loop-condition-expression is evaluated before the each iteration, and the update-expression is evaluated after the each iteration and has to be used to update the loop variable(-s) (which are usually declared in the init-statement, but not necessarily), for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <cassert> #include <iostream> int main() { for (int i = 0; i < 10; ++i) std::cout << "Embarcadero RAD Studio is a powerful IDE!\n"; int i{}; for (; i < 10; ++i) std::cout << "Embarcadero RAD Studio is a powerful IDE!\n"; assert(i == 10); for (;;--i) { if (i < 1) break; std::cout << "Embarcadero RAD Studio is a powerful IDE!\n"; } assert(i == 0); for (;;) { std::cout << "Embarcadero RAD Studio is a powerful IDE!\n"; if (++i > 9) break; } assert(i == 10); } |
Please note, that for
-loop is preferable when there is an obvious loop variable, or where a loop variable is naturally updated after the each iteration.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition