[stmt.iter.general]

# 8 Statements [[stmt]](./#stmt)

## 8.6 Iteration statements [[stmt.iter]](stmt.iter#general)

### 8.6.1 General [stmt.iter.general]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L531)

Iteration statements specify looping[.](#1.sentence-1)

[iteration-statement:](#nt:iteration-statement "8.6.1 General [stmt.iter.general]")  
while ( [*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]") ) [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]")  
do [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]") while ( [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]") ) ;  
for ( [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]") [*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]")opt ; [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]")opt ) [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]")  
for ( [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]")opt [*for-range-declaration*](stmt.pre#nt:for-range-declaration "8.1 Preamble [stmt.pre]") : [*for-range-initializer*](stmt.pre#nt:for-range-initializer "8.1 Preamble [stmt.pre]") ) [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]")

[*Note [1](#note-1)*:

An [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]") ends with a semicolon[.](#1.sentence-2)

â *end note*]

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L550)

The substatement in an [*iteration-statement*](#nt:iteration-statement "8.6.1 General [stmt.iter.general]") implicitly defines
a block [scope](basic.scope "6.4 Scope [basic.scope]") which is entered and exited each time
through the loop[.](#2.sentence-1)

If the substatement in an [*iteration-statement*](#nt:iteration-statement "8.6.1 General [stmt.iter.general]") is
a single statement and not a [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]"),
it is as if it was rewritten to be
a [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") containing the original statement[.](#2.sentence-2)

[*Example [1](#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[.](#2.sentence-4)

â *end example*]

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L573)

A [*trivially empty iteration statement*](#def:iteration_statement,trivially_empty "8.6.1 General [stmt.iter.general]") is
an iteration statement matching one of the following forms:

- [(3.1)](#3.1)

while ( [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]") ) ;

- [(3.2)](#3.2)

while ( [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]") ) { }

- [(3.3)](#3.3)

do ; while ( [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]") ) ;

- [(3.4)](#3.4)

do { } while ( [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]") ) ;

- [(3.5)](#3.5)

for ( [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]") [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]")opt ; ) ;

- [(3.6)](#3.6)

for ( [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]") [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]")opt ; ) { }

The [*controlling expression*](#def:expression,controlling "8.6.1 General [stmt.iter.general]") of a trivially empty iteration statement
is the [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]") of
a while, do, or for statement
(or true, if the for statement has no [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]"))[.](#3.sentence-1)

A [*trivial infinite loop*](#def:loop,trivial_infinite "8.6.1 General [stmt.iter.general]") is a trivially empty iteration statement
for which the converted controlling expression is a constant expression,
when interpreted as a [*constant-expression*](expr.const#nt:constant-expression "7.7 Constant expressions [expr.const]") ([[expr.const]](expr.const "7.7 Constant expressions")), and
evaluates to true[.](#3.sentence-2)

The [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]") of a trivial infinite loop is replaced with
a call to the function std​::​this_thread​::​yield ([[thread.thread.this]](thread.thread.this "32.4.5 Namespace this_­thread"));
it is implementation-defined
whether this replacement occurs on freestanding implementations[.](#3.sentence-3)

[*Note [2](#note-2)*:

In a freestanding environment,
concurrent forward progress is not guaranteed;
such systems therefore require explicit cooperation[.](#3.sentence-4)

A call to yield can add implicit cooperation where none is otherwise intended[.](#3.sentence-5)

â *end note*]
