48 lines
1.3 KiB
Markdown
48 lines
1.3 KiB
Markdown
[stmt.while]
|
||
|
||
# 8 Statements [[stmt]](./#stmt)
|
||
|
||
## 8.6 Iteration statements [[stmt.iter]](stmt.iter#stmt.while)
|
||
|
||
### 8.6.2 The while statement [stmt.while]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L607)
|
||
|
||
In the while statement, the substatement is executed repeatedly
|
||
until the value of the condition ([[stmt.pre]](stmt.pre "8.1 Preamble")) becomesfalse[.](#1.sentence-1)
|
||
|
||
The test takes place before each execution of the
|
||
substatement[.](#1.sentence-2)
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L613)
|
||
|
||
A while statement is equivalent to
|
||
|
||
*label* :
|
||
{
|
||
if ( [*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]") ) {
|
||
[*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]")
|
||
goto *label* ;
|
||
}
|
||
}
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
The variable created in the condition is destroyed and created with each
|
||
iteration of the loop[.](#2.sentence-1)
|
||
|
||
[*Example [1](#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[.](#2.sentence-2)
|
||
|
||
â *end example*]
|
||
|
||
â *end note*]
|