Files
2025-10-25 03:02:53 +03:00

48 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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.1Preamble")) 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.1Preamble[stmt.pre]") ) {
[*statement*](stmt.pre#nt:statement "8.1Preamble[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*]