tmp/tmpqu3_zk4f/{from.md → to.md}
RENAMED
|
@@ -4,42 +4,40 @@ Iteration statements specify looping.
|
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
iteration-statement:
|
| 7 |
'while (' condition ')' statement
|
| 8 |
'do' statement 'while (' expression ') ;'
|
| 9 |
-
'for ('
|
| 10 |
'for (' for-range-declaration ':' for-range-initializer ')' statement
|
| 11 |
```
|
| 12 |
|
| 13 |
-
``` bnf
|
| 14 |
-
for-init-statement:
|
| 15 |
-
expression-statement
|
| 16 |
-
simple-declaration
|
| 17 |
-
```
|
| 18 |
-
|
| 19 |
``` bnf
|
| 20 |
for-range-declaration:
|
| 21 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
|
|
|
| 22 |
```
|
| 23 |
|
| 24 |
``` bnf
|
| 25 |
for-range-initializer:
|
| 26 |
-
|
| 27 |
-
braced-init-list
|
| 28 |
```
|
| 29 |
|
| 30 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 31 |
-
*for-range-declaration*.
|
|
|
|
|
|
|
| 32 |
|
| 33 |
The substatement in an *iteration-statement* implicitly defines a block
|
| 34 |
scope ([[basic.scope]]) which is entered and exited each time through
|
| 35 |
the loop.
|
| 36 |
|
| 37 |
If the substatement in an iteration-statement is a single statement and
|
| 38 |
-
not a *compound-statement
|
| 39 |
compound-statement containing the original statement.
|
| 40 |
|
|
|
|
|
|
|
| 41 |
``` cpp
|
| 42 |
while (--x >= 0)
|
| 43 |
int i;
|
| 44 |
```
|
| 45 |
|
|
@@ -51,12 +49,28 @@ while (--x >= 0) {
|
|
| 51 |
}
|
| 52 |
```
|
| 53 |
|
| 54 |
Thus after the `while` statement, `i` is no longer in scope.
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
|
| 60 |
|
| 61 |
In the `while` statement the substatement is executed repeatedly until
|
| 62 |
the value of the condition ([[stmt.select]]) becomes `false`. The test
|
|
@@ -85,10 +99,12 @@ label:
|
|
| 85 |
```
|
| 86 |
|
| 87 |
The variable created in a condition is destroyed and created with each
|
| 88 |
iteration of the loop.
|
| 89 |
|
|
|
|
|
|
|
| 90 |
``` cpp
|
| 91 |
struct A {
|
| 92 |
int val;
|
| 93 |
A(int i) : val(i) { }
|
| 94 |
~A() { }
|
|
@@ -103,10 +119,12 @@ while (A a = i) {
|
|
| 103 |
|
| 104 |
In the while-loop, the constructor and destructor are each called twice,
|
| 105 |
once for the condition that succeeds and once for the condition that
|
| 106 |
fails.
|
| 107 |
|
|
|
|
|
|
|
| 108 |
### The `do` statement <a id="stmt.do">[[stmt.do]]</a>
|
| 109 |
|
| 110 |
The expression is contextually converted to `bool` (Clause [[conv]]);
|
| 111 |
if that conversion is ill-formed, the program is ill-formed.
|
| 112 |
|
|
@@ -117,102 +135,91 @@ execution of the statement.
|
|
| 117 |
### The `for` statement <a id="stmt.for">[[stmt.for]]</a>
|
| 118 |
|
| 119 |
The `for` statement
|
| 120 |
|
| 121 |
``` bnf
|
| 122 |
-
'for ('
|
| 123 |
```
|
| 124 |
|
| 125 |
is equivalent to
|
| 126 |
|
| 127 |
-
except that names declared in the *
|
| 128 |
declarative region as those declared in the *condition*, and except that
|
| 129 |
a `continue` in *statement* (not enclosed in another iteration
|
| 130 |
statement) will execute *expression* before re-evaluating *condition*.
|
| 131 |
-
Thus the first statement specifies initialization for the loop; the
|
| 132 |
-
condition ([[stmt.select]]) specifies a test, made before each
|
| 133 |
-
iteration, such that the loop is exited when the condition becomes
|
| 134 |
-
`false`; the expression often specifies incrementing that is done after
|
| 135 |
-
each iteration.
|
| 136 |
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
missing *condition* makes the implied `while` clause equivalent to
|
| 139 |
`while(true)`.
|
| 140 |
|
| 141 |
-
If the *
|
| 142 |
declared extends to the end of the `for` statement.
|
| 143 |
|
|
|
|
|
|
|
| 144 |
``` cpp
|
| 145 |
int i = 42;
|
| 146 |
int a[10];
|
| 147 |
|
| 148 |
for (int i = 0; i < 10; i++)
|
| 149 |
a[i] = i;
|
| 150 |
|
| 151 |
int j = i; // j = 42
|
| 152 |
```
|
| 153 |
|
|
|
|
|
|
|
| 154 |
### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
|
| 155 |
|
| 156 |
-
|
| 157 |
|
| 158 |
``` bnf
|
| 159 |
-
'for (' for-range-declaration :
|
| 160 |
```
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
```
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
```
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
__begin != __end;
|
| 184 |
-
++__begin ) {
|
| 185 |
-
for-range-declaration = *__begin;
|
| 186 |
-
statement
|
| 187 |
-
}
|
| 188 |
-
}
|
| 189 |
-
```
|
| 190 |
-
|
| 191 |
-
where `__range`, `__begin`, and `__end` are variables defined for
|
| 192 |
-
exposition only, and `_RangeT` is the type of the *expression*, and
|
| 193 |
-
*begin-expr* and *end-expr* are determined as follows:
|
| 194 |
-
|
| 195 |
-
- if `_RangeT` is an array type, *begin-expr* and *end-expr* are
|
| 196 |
-
`__range` and `__range + __bound`, respectively, where `__bound` is
|
| 197 |
-
the array bound. If `_RangeT` is an array of unknown size or an array
|
| 198 |
-
of incomplete type, the program is ill-formed;
|
| 199 |
-
- if `_RangeT` is a class type, the *unqualified-id*s `begin` and `end`
|
| 200 |
-
are looked up in the scope of class `\mbox{_RangeT}` as if by class
|
| 201 |
-
member access lookup ([[basic.lookup.classref]]), and if either (or
|
| 202 |
-
both) finds at least one declaration, *begin-expr* and *end-expr* are
|
| 203 |
-
`__range.begin()` and `__range.end()`, respectively;
|
| 204 |
- otherwise, *begin-expr* and *end-expr* are `begin(__range)` and
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
``` cpp
|
| 210 |
int array[5] = { 1, 2, 3, 4, 5 };
|
| 211 |
for (int& x : array)
|
| 212 |
x *= 2;
|
| 213 |
```
|
| 214 |
|
|
|
|
|
|
|
| 215 |
In the *decl-specifier-seq* of a *for-range-declaration*, each
|
| 216 |
*decl-specifier* shall be either a *type-specifier* or `constexpr`. The
|
| 217 |
*decl-specifier-seq* shall not define a class or enumeration.
|
| 218 |
|
|
|
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
iteration-statement:
|
| 7 |
'while (' condition ')' statement
|
| 8 |
'do' statement 'while (' expression ') ;'
|
| 9 |
+
'for (' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
|
| 10 |
'for (' for-range-declaration ':' for-range-initializer ')' statement
|
| 11 |
```
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
``` bnf
|
| 14 |
for-range-declaration:
|
| 15 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
| 16 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ '[' identifier-list ']'
|
| 17 |
```
|
| 18 |
|
| 19 |
``` bnf
|
| 20 |
for-range-initializer:
|
| 21 |
+
expr-or-braced-init-list
|
|
|
|
| 22 |
```
|
| 23 |
|
| 24 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 25 |
+
*for-range-declaration*.
|
| 26 |
+
|
| 27 |
+
[*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
|
| 28 |
|
| 29 |
The substatement in an *iteration-statement* implicitly defines a block
|
| 30 |
scope ([[basic.scope]]) which is entered and exited each time through
|
| 31 |
the loop.
|
| 32 |
|
| 33 |
If the substatement in an iteration-statement is a single statement and
|
| 34 |
+
not a *compound-statement*, it is as if it was rewritten to be a
|
| 35 |
compound-statement containing the original statement.
|
| 36 |
|
| 37 |
+
[*Example 1*:
|
| 38 |
+
|
| 39 |
``` cpp
|
| 40 |
while (--x >= 0)
|
| 41 |
int i;
|
| 42 |
```
|
| 43 |
|
|
|
|
| 49 |
}
|
| 50 |
```
|
| 51 |
|
| 52 |
Thus after the `while` statement, `i` is no longer in scope.
|
| 53 |
|
| 54 |
+
— *end example*]
|
| 55 |
+
|
| 56 |
+
If a name introduced in an *init-statement* or *for-range-declaration*
|
| 57 |
+
is redeclared in the outermost block of the substatement, the program is
|
| 58 |
+
ill-formed.
|
| 59 |
+
|
| 60 |
+
[*Example 2*:
|
| 61 |
+
|
| 62 |
+
``` cpp
|
| 63 |
+
void f() {
|
| 64 |
+
for (int i = 0; i < 10; ++i)
|
| 65 |
+
int i = 0; // error: redeclaration
|
| 66 |
+
for (int i : { 1, 2, 3 })
|
| 67 |
+
int i = 1; // error: redeclaration
|
| 68 |
+
}
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
— *end example*]
|
| 72 |
|
| 73 |
### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
|
| 74 |
|
| 75 |
In the `while` statement the substatement is executed repeatedly until
|
| 76 |
the value of the condition ([[stmt.select]]) becomes `false`. The test
|
|
|
|
| 99 |
```
|
| 100 |
|
| 101 |
The variable created in a condition is destroyed and created with each
|
| 102 |
iteration of the loop.
|
| 103 |
|
| 104 |
+
[*Example 1*:
|
| 105 |
+
|
| 106 |
``` cpp
|
| 107 |
struct A {
|
| 108 |
int val;
|
| 109 |
A(int i) : val(i) { }
|
| 110 |
~A() { }
|
|
|
|
| 119 |
|
| 120 |
In the while-loop, the constructor and destructor are each called twice,
|
| 121 |
once for the condition that succeeds and once for the condition that
|
| 122 |
fails.
|
| 123 |
|
| 124 |
+
— *end example*]
|
| 125 |
+
|
| 126 |
### The `do` statement <a id="stmt.do">[[stmt.do]]</a>
|
| 127 |
|
| 128 |
The expression is contextually converted to `bool` (Clause [[conv]]);
|
| 129 |
if that conversion is ill-formed, the program is ill-formed.
|
| 130 |
|
|
|
|
| 135 |
### The `for` statement <a id="stmt.for">[[stmt.for]]</a>
|
| 136 |
|
| 137 |
The `for` statement
|
| 138 |
|
| 139 |
``` bnf
|
| 140 |
+
'for (' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
|
| 141 |
```
|
| 142 |
|
| 143 |
is equivalent to
|
| 144 |
|
| 145 |
+
except that names declared in the *init-statement* are in the same
|
| 146 |
declarative region as those declared in the *condition*, and except that
|
| 147 |
a `continue` in *statement* (not enclosed in another iteration
|
| 148 |
statement) will execute *expression* before re-evaluating *condition*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
[*Note 1*: Thus the first statement specifies initialization for the
|
| 151 |
+
loop; the condition ([[stmt.select]]) specifies a test, sequenced
|
| 152 |
+
before each iteration, such that the loop is exited when the condition
|
| 153 |
+
becomes `false`; the expression often specifies incrementing that is
|
| 154 |
+
sequenced after each iteration. — *end note*]
|
| 155 |
+
|
| 156 |
+
Either or both of the *condition* and the *expression* can be omitted. A
|
| 157 |
missing *condition* makes the implied `while` clause equivalent to
|
| 158 |
`while(true)`.
|
| 159 |
|
| 160 |
+
If the *init-statement* is a declaration, the scope of the name(s)
|
| 161 |
declared extends to the end of the `for` statement.
|
| 162 |
|
| 163 |
+
[*Example 1*:
|
| 164 |
+
|
| 165 |
``` cpp
|
| 166 |
int i = 42;
|
| 167 |
int a[10];
|
| 168 |
|
| 169 |
for (int i = 0; i < 10; i++)
|
| 170 |
a[i] = i;
|
| 171 |
|
| 172 |
int j = i; // j = 42
|
| 173 |
```
|
| 174 |
|
| 175 |
+
— *end example*]
|
| 176 |
+
|
| 177 |
### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
|
| 178 |
|
| 179 |
+
The range-based `for` statement
|
| 180 |
|
| 181 |
``` bnf
|
| 182 |
+
'for (' for-range-declaration ':' for-range-initializer ')' statement
|
| 183 |
```
|
| 184 |
|
| 185 |
+
is equivalent to
|
| 186 |
+
|
| 187 |
+
where
|
| 188 |
+
|
| 189 |
+
- if the *for-range-initializer* is an *expression*, it is regarded as
|
| 190 |
+
if it were surrounded by parentheses (so that a comma operator cannot
|
| 191 |
+
be reinterpreted as delimiting two *init-declarator*s);
|
| 192 |
+
- `__range`, `__begin`, and `__end` are variables defined for exposition
|
| 193 |
+
only; and
|
| 194 |
+
- *begin-expr* and *end-expr* are determined as follows:
|
| 195 |
+
- if the *for-range-initializer* is an expression of array type `R`,
|
| 196 |
+
*begin-expr* and *end-expr* are `__range` and `__range + __bound`,
|
| 197 |
+
respectively, where `__bound` is the array bound. If `R` is an array
|
| 198 |
+
of unknown bound or an array of incomplete type, the program is
|
| 199 |
+
ill-formed;
|
| 200 |
+
- if the *for-range-initializer* is an expression of class type `C`,
|
| 201 |
+
the *unqualified-id*s `begin` and `end` are looked up in the scope
|
| 202 |
+
of `C` as if by class member access lookup (
|
| 203 |
+
[[basic.lookup.classref]]), and if either (or both) finds at least
|
| 204 |
+
one declaration, *begin-expr* and *end-expr* are `__range.begin()`
|
| 205 |
+
and `__range.end()`, respectively;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
- otherwise, *begin-expr* and *end-expr* are `begin(__range)` and
|
| 207 |
+
`end(__range)`, respectively, where `begin` and `end` are looked up
|
| 208 |
+
in the associated namespaces ([[basic.lookup.argdep]]).
|
| 209 |
+
\[*Note 1*: Ordinary unqualified lookup ([[basic.lookup.unqual]])
|
| 210 |
+
is not performed. — *end note*]
|
| 211 |
+
|
| 212 |
+
[*Example 1*:
|
| 213 |
|
| 214 |
``` cpp
|
| 215 |
int array[5] = { 1, 2, 3, 4, 5 };
|
| 216 |
for (int& x : array)
|
| 217 |
x *= 2;
|
| 218 |
```
|
| 219 |
|
| 220 |
+
— *end example*]
|
| 221 |
+
|
| 222 |
In the *decl-specifier-seq* of a *for-range-declaration*, each
|
| 223 |
*decl-specifier* shall be either a *type-specifier* or `constexpr`. The
|
| 224 |
*decl-specifier-seq* shall not define a class or enumeration.
|
| 225 |
|