Files
cppdraft_translate/cppdraft/stmt/for.md
2025-10-25 03:02:53 +03:00

50 lines
2.3 KiB
Markdown
Raw 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.for]
# 8 Statements [[stmt]](./#stmt)
## 8.6 Iteration statements [[stmt.iter]](stmt.iter#stmt.for)
### 8.6.4 The for statement [stmt.for]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L663)
The for statement
for ( [*init-statement*](stmt.pre#nt:init-statement "8.1Preamble[stmt.pre]") [*condition*](stmt.pre#nt:condition "8.1Preamble[stmt.pre]")opt ; [*expression*](expr.comma#nt:expression "7.6.20Comma operator[expr.comma]")opt ) [*statement*](stmt.pre#nt:statement "8.1Preamble[stmt.pre]")
is equivalent to
{
[*init-statement*](stmt.pre#nt:init-statement "8.1Preamble[stmt.pre]")
while ( [*condition*](stmt.pre#nt:condition "8.1Preamble[stmt.pre]") ) {
[*statement*](stmt.pre#nt:statement "8.1Preamble[stmt.pre]")
[*expression*](expr.comma#nt:expression "7.6.20Comma operator[expr.comma]") ;
}
}
except that the [*init-statement*](stmt.pre#nt:init-statement "8.1Preamble[stmt.pre]") is
in the same scope as the [*condition*](stmt.pre#nt:condition "8.1Preamble[stmt.pre]"), and
except that acontinue in [*statement*](stmt.pre#nt:statement "8.1Preamble[stmt.pre]") (not enclosed in another
iteration statement) will execute [*expression*](expr.comma#nt:expression "7.6.20Comma operator[expr.comma]") before
re-evaluating [*condition*](stmt.pre#nt:condition "8.1Preamble[stmt.pre]")[.](#1.sentence-1)
[*Note [1](#note-1)*:
Thus the first statement specifies initialization for the loop; the
condition ([[stmt.pre]](stmt.pre "8.1Preamble")) 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[.](#1.sentence-2)
— *end note*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L693)
Either or both of the [*condition*](stmt.pre#nt:condition "8.1Preamble[stmt.pre]") and the [*expression*](expr.comma#nt:expression "7.6.20Comma operator[expr.comma]") can be omitted[.](#2.sentence-1)
A missing [*condition*](stmt.pre#nt:condition "8.1Preamble[stmt.pre]") makes the implied while clause
equivalent to while (true)[.](#2.sentence-2)