[stmt.iter] (original) (raw)

8 Statements [stmt.stmt]

8.6 Iteration statements [stmt.iter]

8.6.1 General [stmt.iter.general]

Iteration statements specify looping.

The substatement in an iteration-statement implicitly defines a block scope which is entered and exited each time through the loop.

If the substatement in an iteration-statement is a single statement and not a compound-statement, it is as if it was rewritten to be a compound-statement containing the original statement.

[Example 1:

while (--x >= 0) int i;can be equivalently rewritten aswhile (--x >= 0) { int i;}

Thus after the while statement, i is no longer in scope.

— _end example_]

If a name introduced in aninit-statement or for-range-declarationis redeclared in the outermost block of the substatement, the program is ill-formed.

[Example 2: void f() { for (int i = 0; i < 10; ++i) int i = 0; for (int i : { 1, 2, 3 }) int i = 1; } — _end example_]

8.6.2 The while statement [stmt.while]

In the while statement the substatement is executed repeatedly until the value of the condition ([stmt.select]) becomesfalse.

The test takes place before each execution of the substatement.

When the condition of a while statement is a declaration, the scope of the variable that is declared extends from its point of declaration ([basic.scope.pdecl]) to the end of the while statement.

A while statement is equivalent to

[Note 1:

The variable created in the condition is destroyed and created with each iteration of the loop.

[Example 1: struct A { int val; A(int i) : val(i) { } ~A() { } operator bool() { return val != 0; } };int i = 1;while (A a = i) { i = 0;}

In the while-loop, the constructor and destructor are each called twice, once for the condition that succeeds and once for the condition that fails.

— _end example_]

— _end note_]

8.6.3 The do statement [stmt.do]

In the do statement the substatement is executed repeatedly until the value of the expression becomes false.

The test takes place after each execution of the statement.

8.6.4 The for statement [stmt.for]

The for statement

is equivalent to

except that names declared in the init-statement are in the same declarative region as those declared in thecondition, and except that acontinue in statement (not enclosed in another iteration statement) will execute expression before re-evaluating condition.

[Note 1:

Thus the first statement specifies initialization for the loop; the condition ([stmt.select]) specifies a test, sequenced before each iteration, such that the loop is exited when the condition becomesfalse; the expression often specifies incrementing that is sequenced after each iteration.

— _end note_]

A missing conditionmakes the implied while clause equivalent to while(true).

If the init-statement is a declaration, the scope of the name(s) declared extends to the end of the for statement.

[Example 1: int i = 42;int a[10];for (int i = 0; i < 10; i++) a[i] = i;int j = i; — _end example_]

8.6.5 The range-based for statement [stmt.ranged]

The range-based for statement

is equivalent to

{
init-statement
auto &&range = for-range-initializer ;
auto begin = begin-expr ;
auto end = end-expr ;
for ( ; begin != end; ++begin ) {
for-range-declaration = * begin ;
statement
}
}

where

[Example 1: int array[5] = { 1, 2, 3, 4, 5 };for (int& x : array) x *= 2; — _end example_]