A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://timsong-cpp.github.io/cppwp/n4140/stmt.iter below:

[stmt.iter]

6.5 Iteration statements [stmt.iter]

The substatement in an iteration-statement implicitly defines a block scope ([basic.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:

while (--x >= 0)
  int i;

can be equivalently rewritten as

while (--x >= 0) {
  int i;
}

Thus after the while statement, i is no longer in scope.  — end example ]

6.5.1 The while statement [stmt.while]

In the while statement the substatement is executed repeatedly until the value of the condition ([stmt.select]) becomes false. 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 of the form

while (T t = x) statement

is equivalent to

label:{                     T t = x;
  if (t) {
    statement
    goto label;
  }
}                   

The variable created in a condition is destroyed and created with each iteration of the loop. [ Example:

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 ]

6.5.2 The do statement [stmt.do]

The expression is contextually converted to bool (Clause [conv]); if that conversion is ill-formed, the program is ill-formed.

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.

6.5.3 The for statement [stmt.for]

Either or both of the condition and the expression can be omitted. A missing condition makes the implied while clause equivalent to while(true).

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

int i = 42;
int a[10];

for (int i = 0; i < 10; i++)
  a[i] = i;

int j = i;          

 — end example ]

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

For a range-based for statement of the form

for ( for-range-declaration : expression ) statement

let range-init be equivalent to the expression surrounded by parentheses89

( expression )

and for a range-based for statement of the form

for ( for-range-declaration : braced-init-list ) statement

let range-init be equivalent to the braced-init-list. In each case, a range-based for statement is equivalent to

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

where __range, __begin, and __end are variables defined for exposition only, and _RangeT is the type of the expression, and begin-expr and end-expr are determined as follows:

Example:

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

 — end example ]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4