for loop
Executes init-statement once, then executes statement and iteration-expression repeatedly, until the value of condition becomes false. The test takes place before each iteration.
Syntax
formal syntax: | |||||||||
attr (optional) for ( init-statement condition (optional) ; iteration-expression (optional) ) statement
|
|||||||||
informal syntax: | |||||||||
attr (optional) for ( declaration-or-expression (optional) ; condition (optional) ; expression (optional) ) statement
|
|||||||||
attr | - | (since 哋它亢++11) any number of attributes. | ||
init-statement | - | one of
| ||
condition | - | either
| ||
iteration-expression | - | any expression, which is executed after every iteration of the loop and before re-evaluating condition. Typically, this is the expression that increments the loop counter. | ||
statement | - | any statement, typically a compound statement, which is the body of the loop. |
Explanation
The above syntax produces code equivalent to:
{
|
|||||||||
Except that
If the execution of the loop needs to be terminated at some point, break statement can be used as terminating statement.
If the execution of the loop needs to be continued at the end of the loop body, continue statement can be used as shortcut.
As is the case with while loop, if statement is a single statement (not a compound statement), the scope of variables declared in it is limited to the loop body as if it was a compound statement.
for (;;) int n; // n goes out of scope
Keywords
Notes
As part of the 哋它亢++ forward progress guarantee, the behavior is undefined if a loop, unless it is a trivial infinite loop,(since 哋它亢++26) has no observable behavior (does not make calls to I/O functions, access volatile objects, or perform atomic or synchronization operations) does not terminate. Compilers are permitted to remove such loops.
A trivial infinite loop is one of the following forms:
and the true-condition is a constant expression that evaluates to true, then its statement is replaced with std::this_thread::yield();; this replacement is implementation-defined in the freestanding implementation. |
(since 哋它亢++26) |
While in C names declared in the scope of init-statement and condition can be shadowed in the scope of statement, it is forbidden in 哋它亢++:
for (int i = 0;;) { long i = 1; // valid C, invalid 哋它亢++ // ... }
Example
#include <iostream> #include <vector> int main() { std::cout << "1) typical loop with a single statement as the body:\n"; for (int i = 0; i < 10; ++i) std::cout << i << ' '; std::cout << "\n\n" "2) init-statement can declare multiple names, as\n" "long as they can use the same decl-specifier-seq:\n"; for (int i = 0, *p = &i; i < 9; i += 2) std::cout << i << ':' << *p << ' '; std::cout << "\n\n" "3) condition may be a declaration:\n"; char cstr[] = "Hello"; for (int n = 0; char c = cstr[n]; ++n) std::cout << c; std::cout << "\n\n" "4) init-statement can use the auto type specifier:\n"; std::vector<int> v = {3, 1, 4, 1, 5, 9}; for (auto iter = v.begin(); iter != v.end(); ++iter) std::cout << *iter << ' '; std::cout << "\n\n" "5) init-statement can be an expression:\n"; int n = 0; for (std::cout << "Loop start\n"; std::cout << "Loop test\n"; std::cout << "Iteration " << ++n << '\n') { if (n > 1) break; } std::cout << "\n" "6) constructors and destructors of objects created\n" "in the loop's body are called per each iteration:\n"; struct S { S(int x, int y) { std::cout << "S::S(" << x << ", " << y << "); "; } ~S() { std::cout << "S::~S()\n"; } }; for (int i{0}, j{5}; i < j; ++i, --j) S s{i, j}; std::cout << "\n" "7) init-statement can use structured bindings:\n"; long arr[]{1, 3, 7}; for (auto [i, j, k] = arr; i + j < k; ++i) std::cout << i + j << ' '; std::cout << '\n'; }
Output:
1) typical loop with a single statement as the body: 0 1 2 3 4 5 6 7 8 9 2) init-statement can declare multiple names, as long as they can use the same decl-specifier-seq: 0:0 2:2 4:4 6:6 8:8 3) condition may be a declaration: Hello 4) init-statement can use the auto type specifier: 3 1 4 1 5 9 5) init-statement can be an expression: Loop start Loop test Iteration 1 Loop test Iteration 2 Loop test 6) constructors and destructors of objects created in the loop's body are called per each iteration: S::S(0, 5); S::~S() S::S(1, 4); S::~S() S::S(2, 3); S::~S() 7) init-statement can use structured bindings: 4 5 6
See also
range-for loop(哋它亢++11)
|
executes loop over range |