forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatements.tex
1265 lines (1137 loc) · 42.9 KB
/
statements.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[stmt.stmt]{Statements}%
\indextext{statement|(}
\gramSec[gram.stmt]{Statements}
\indextext{block (statement)|see{statement, compound}}
\rSec1[stmt.pre]{Preamble}
\pnum
Except as indicated, statements are executed in sequence\iref{intro.execution}.
\begin{bnf}
\nontermdef{statement}\br
labeled-statement\br
\opt{attribute-specifier-seq} expression-statement\br
\opt{attribute-specifier-seq} compound-statement\br
\opt{attribute-specifier-seq} selection-statement\br
\opt{attribute-specifier-seq} iteration-statement\br
\opt{attribute-specifier-seq} jump-statement\br
declaration-statement\br
\opt{attribute-specifier-seq} try-block
\end{bnf}
\begin{bnf}
\nontermdef{init-statement}\br
expression-statement\br
simple-declaration\br
alias-declaration
\end{bnf}
\begin{bnf}
\nontermdef{condition}\br
expression\br
\opt{attribute-specifier-seq} decl-specifier-seq declarator brace-or-equal-initializer\br
structured-binding-declaration initializer
\end{bnf}
The optional \grammarterm{attribute-specifier-seq} appertains to the respective statement.
\pnum
A \defn{substatement} of a \grammarterm{statement} is one of the following:
\begin{itemize}
\item
for a \grammarterm{labeled-statement}, its \grammarterm{statement},
\item
for a \grammarterm{compound-statement}, any \grammarterm{statement} of its \grammarterm{statement-seq},
\item
for a \grammarterm{selection-statement}, any of its \grammarterm{statement}{s} or \grammarterm{compound-statement}{s} (but not its \grammarterm{init-statement}), or
\item
for an \grammarterm{iteration-statement}, its \grammarterm{statement} (but not an \grammarterm{init-statement}).
\end{itemize}
\begin{note}
The \grammarterm{compound-statement} of a \grammarterm{lambda-expression}
is not a substatement of the \grammarterm{statement} (if any)
in which the \grammarterm{lambda-expression} lexically appears.
\end{note}
\pnum
\indextext{statement!enclosing}%
A \grammarterm{statement} \tcode{S1} \defnx{encloses}{enclosing statement}
a \grammarterm{statement} \tcode{S2} if
\begin{itemize}
\item
\tcode{S2} is a substatement of \tcode{S1},
\item
\tcode{S1} is a \grammarterm{selection-statement} or
\grammarterm{iteration-statement} and
\tcode{S2} is the \grammarterm{init-statement} of \tcode{S1},
\item
\tcode{S1} is a \grammarterm{try-block} and \tcode{S2}
is its \grammarterm{compound-statement} or
any of the \grammarterm{compound-statement}{s} of
its \grammarterm{handler}{s}, or
\item
\tcode{S1} encloses a statement \tcode{S3} and \tcode{S3} encloses \tcode{S2}.
\end{itemize}
\indextext{statement!enclosed by}%
A statement \tcode{S1} is
\defnx{enclosed by}{enclosed by statement}
a statement \tcode{S2} if
\tcode{S2} encloses \tcode{S1}.
\pnum
\indextext{\idxgram{condition}{s}!rules for}%
The rules for \grammarterm{condition}{s} apply both
to \grammarterm{selection-statement}{s}\iref{stmt.select} and
to the \keyword{for} and \keyword{while} statements\iref{stmt.iter}.
If a \grammarterm{structured-binding-declaration}
appears in a \grammarterm{condition},
the \grammarterm{condition} is a structured binding declaration\iref{dcl.pre}.
A \grammarterm{condition} that is
neither an \grammarterm{expression} nor a structured binding declaration
is a declaration\iref{dcl.dcl}.
The \grammarterm{declarator} shall not
specify a function or an array. The \grammarterm{decl-specifier-seq} shall not
define a class or enumeration. If the \keyword{auto} \grammarterm{type-specifier} appears in
the \grammarterm{decl-specifier-seq},
the type of the identifier being declared is deduced from the initializer as described in~\ref{dcl.spec.auto}.
\pnum
The \defnadj{decision}{variable} of a \grammarterm{condition}
that is neither an \grammarterm{expression} nor a structured binding declaration
is the declared variable.
The decision variable of a \grammarterm{condition}
that is a structured binding declaration is specified in \ref{dcl.struct.bind}.
\pnum
The value of a \grammarterm{condition} that is not an \grammarterm{expression}
in a statement other than a \keyword{switch} statement is the value of the
decision variable
contextually converted to \tcode{bool}\iref{conv}.
If that
conversion is ill-formed, the program is ill-formed.
The value of a
\grammarterm{condition} that is an expression is the value of the
expression, contextually converted to \tcode{bool}
for statements other
than \keyword{switch};
if that conversion is ill-formed, the program is
ill-formed. The value of the condition will be referred to as simply
``the condition'' where the usage is unambiguous.
\pnum
If a \grammarterm{condition} can be syntactically resolved
as either an expression or a declaration,
it is interpreted as the latter.
\pnum
In the \grammarterm{decl-specifier-seq} of a \grammarterm{condition},
including that of any \grammarterm{structured-binding-declaration} of
the \grammarterm{condition},
each
\grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
or \keyword{constexpr}.
\rSec1[stmt.label]{Label}%
\indextext{statement!labeled}
\pnum
\indextext{statement!labeled}%
\indextext{\idxcode{:}!label specifier}%
A label can be added to a statement or
used anywhere in a \grammarterm{compound-statement}.
\begin{bnf}
\nontermdef{label}\br
\opt{attribute-specifier-seq} identifier \terminal{:}\br
\opt{attribute-specifier-seq} \keyword{case} constant-expression \terminal{:}\br
\opt{attribute-specifier-seq} \keyword{default} \terminal{:}
\end{bnf}
\begin{bnf}
\nontermdef{labeled-statement}\br
label statement
\end{bnf}
The optional \grammarterm{attribute-specifier-seq} appertains to the label.
\indextext{statement!\idxcode{goto}}%
The only use of a label with an \grammarterm{identifier} is
as the target of a \tcode{goto}.
\indextext{label!scope of}%
No two labels in a function shall have the same \grammarterm{identifier}.
A label can be used in a \tcode{goto} statement
before its introduction.
\pnum
\indextext{\idxgram{labeled-statement}}%
\indextext{label!\idxcode{case}}%
\indextext{label!\idxcode{default}}%
A \grammarterm{labeled-statement}
whose \grammarterm{label} is a \keyword{case} or \keyword{default} label
shall be enclosed by\iref{stmt.pre} a \keyword{switch} statement\iref{stmt.switch}.
\pnum
A \defnadj{control-flow-limited}{statement} is a statement \tcode{S} for which:
\begin{itemize}
\item
a \keyword{case} or \keyword{default} label appearing within \tcode{S} shall
be associated with a \keyword{switch} statement\iref{stmt.switch} within
\tcode{S}, and
\item
a label declared in \tcode{S} shall only be referred to by a
statement\iref{stmt.goto} in \tcode{S}.
\end{itemize}
\rSec1[stmt.expr]{Expression statement}%
\indextext{statement!expression}
\pnum
Expression statements have the form
\begin{bnf}
\nontermdef{expression-statement}\br
\opt{expression} \terminal{;}
\end{bnf}
The expression is
a discarded-value expression\iref{expr.context}.
All
\indextext{side effects}%
side effects from an expression statement
are completed before the next statement is executed.
\indextext{statement!empty}%
An expression statement with the \grammarterm{expression} missing is called
a \defnadj{null}{statement}.
\begin{note}
Most statements are expression statements --- usually assignments or
function calls. A null statement is useful to supply a null body to an
iteration statement such as a \keyword{while}
statement\iref{stmt.while}.
\end{note}
\rSec1[stmt.block]{Compound statement or block}%
\indextext{\idxcode{\{\}}!block statement}%
\pnum
A \defnadj{compound}{statement} (also known as a block) groups a
sequence of statements into a single statement.
\begin{bnf}
\nontermdef{compound-statement}\br
\terminal{\{} \opt{statement-seq} \opt{label-seq} \terminal{\}}
\end{bnf}
\begin{bnf}
\nontermdef{statement-seq}\br
statement\br
statement-seq statement
\end{bnf}
\begin{bnf}
\nontermdef{label-seq}\br
label\br
label-seq label
\end{bnf}
A label at the end of a \grammarterm{compound-statement}
is treated as if it were followed by a null statement.
\pnum
\begin{note}
A compound statement defines a block scope\iref{basic.scope}.
A declaration is a \grammarterm{statement}\iref{stmt.dcl}.
\end{note}
\rSec1[stmt.select]{Selection statements}%
\rSec2[stmt.select.general]{General}%
\indextext{statement!selection|(}
\pnum
Selection statements choose one of several flows of control.
\indextext{statement!\idxcode{if}}%
\indextext{statement!\idxcode{switch}}%
%
\begin{bnf}
\nontermdef{selection-statement}\br
\keyword{if} \opt{\keyword{constexpr}} \terminal{(} \opt{init-statement} condition \terminal{)} statement\br
\keyword{if} \opt{\keyword{constexpr}} \terminal{(} \opt{init-statement} condition \terminal{)} statement \keyword{else} statement\br
\keyword{if} \opt{\terminal{!}} \keyword{consteval} compound-statement\br
\keyword{if} \opt{\terminal{!}} \keyword{consteval} compound-statement \keyword{else} statement\br
\keyword{switch} \terminal{(} \opt{init-statement} condition \terminal{)} statement
\end{bnf}
See~\ref{dcl.meaning} for the optional \grammarterm{attribute-specifier-seq} in a condition.
\begin{note}
An \grammarterm{init-statement} ends with a semicolon.
\end{note}
\pnum
\indextext{scope!\idxgram{selection-statement}}%
\begin{note}
Each \grammarterm{selection-statement} and
each substatement of a \grammarterm{selection-statement}
has a block scope\iref{basic.scope.block}.
\end{note}
\rSec2[stmt.if]{The \keyword{if} statement}%
\indextext{statement!\idxcode{if}}
\pnum
If the condition\iref{stmt.pre} yields \tcode{true}, the first
substatement is executed. If the \keyword{else} part of the selection
statement is present and the condition yields \tcode{false}, the second
substatement is executed. If the first substatement is reached via a
label, the condition is not evaluated and the second substatement is
not executed. In the second form of \keyword{if} statement
(the one including \keyword{else}), if the first substatement is also an
\keyword{if} statement then that inner \tcode{if} statement shall contain
an \keyword{else} part.
\begin{footnote}
In other words, the \keyword{else} is associated with the nearest un-elsed
\keyword{if}.
\end{footnote}
\pnum
If the \keyword{if} statement is of the form \tcode{if constexpr},
the value of the condition
is contextually converted to \keyword{bool} and
the converted expression shall be a constant expression\iref{expr.const};
this
form is called a \defn{constexpr if} statement. If the value of the
converted condition is \tcode{false}, the first substatement is a
\defn{discarded statement}, otherwise the second substatement, if
present, is a discarded statement. During the instantiation of an
enclosing templated entity\iref{temp.pre}, if the condition is
not value-dependent after its instantiation, the discarded substatement
(if any) is not instantiated.
Each substatement of a constexpr if statement is a control-flow-limited
statement\iref{stmt.label}.
\begin{example}
\begin{codeblock}
if constexpr (sizeof(int[2])) {} // OK, narrowing allowed
\end{codeblock}
\end{example}
\begin{note}
Odr-uses\iref{term.odr.use} in a discarded statement do not require
an entity to be defined.
\end{note}
\begin{example}
\begin{codeblock}
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
// ... handle \tcode{p}
if constexpr (sizeof...(rs) > 0)
g(rs...); // never instantiated with an empty argument list
}
extern int x; // no definition of \tcode{x} required
int f() {
if constexpr (true)
return 0;
else if (x)
return x;
else
return -x;
}
\end{codeblock}
\end{example}
\pnum
An \keyword{if} statement of the form
\begin{ncsimplebnf}
\keyword{if} \opt{\keyword{constexpr}} \terminal{(} init-statement condition \terminal{)} statement
\end{ncsimplebnf}
is equivalent to
\begin{ncsimplebnf}
\terminal{\{}\br
\bnfindent init-statement\br
\bnfindent \keyword{if} \opt{\keyword{constexpr}} \terminal{(} condition \terminal{)} statement\br
\terminal{\}}
\end{ncsimplebnf}
and an \keyword{if} statement of the form
\begin{ncsimplebnf}
\keyword{if} \opt{\keyword{constexpr}} \terminal{(} init-statement condition \terminal{)} statement \keyword{else} statement
\end{ncsimplebnf}
is equivalent to
\begin{ncsimplebnf}
\terminal{\{}\br
\bnfindent init-statement\br
\bnfindent \keyword{if} \opt{\keyword{constexpr}} \terminal{(} condition \terminal{)} statement \keyword{else} statement\br
\terminal{\}}
\end{ncsimplebnf}
except that the \grammarterm{init-statement} is
in the same scope as the \grammarterm{condition}.
\pnum
An \keyword{if} statement of the form \tcode{\keyword{if} \keyword{consteval}}
is called a \defnadj{consteval if}{statement}.
The \grammarterm{statement}, if any, in a consteval if statement
shall be a \grammarterm{compound-statement}.
\begin{example}
\begin{codeblock}
constexpr void f(bool b) {
if (true)
if consteval { }
else ; // error: not a \grammarterm{compound-statement}; \keyword{else} not associated with outer \keyword{if}
}
\end{codeblock}
\end{example}
\pnum
If a consteval if statement is evaluated in a context
that is manifestly constant-evaluated\iref{expr.const},
the first substatement is executed.
\begin{note}
The first substatement is an immediate function context.
\end{note}
Otherwise, if the \keyword{else} part of the selection statement is present,
then the second substatement is executed.
Each substatement of a consteval if statement is a control-flow-limited
statement\iref{stmt.label}.
\pnum
An \keyword{if} statement of the form
\begin{ncsimplebnf}
\keyword{if} \terminal{!} \keyword{consteval} compound-statement
\end{ncsimplebnf}
is not itself a consteval if statement,
but is equivalent to the consteval if statement
\begin{ncsimplebnf}
\keyword{if} \keyword{consteval} \terminal{\{} \terminal{\}} \keyword{else} compound-statement
\end{ncsimplebnf}
An \keyword{if} statement of the form
\begin{ncsimplebnf}
\keyword{if} \terminal{!} \keyword{consteval} compound-statement$_1$ \keyword{else} statement$_2$
\end{ncsimplebnf}
is not itself a consteval if statement,
but is equivalent to the consteval if statement
\begin{ncsimplebnf}
\keyword{if} \keyword{consteval} statement$_2$ \keyword{else} compound-statement$_1$
\end{ncsimplebnf}
\rSec2[stmt.switch]{The \keyword{switch} statement}%
\indextext{statement!\idxcode{switch}}
\pnum
The \keyword{switch} statement causes control to be transferred to one of
several statements depending on the value of a condition.
\pnum
If the \grammarterm{condition} is an \grammarterm{expression},
the value of the condition is the value of the \grammarterm{expression};
otherwise, it is the value of the decision variable.
The value of the condition shall be of integral type, enumeration type, or class
type. If of class type, the
condition is contextually implicitly converted\iref{conv} to
an integral or enumeration type.
If the (possibly converted) type is subject to integral
promotions\iref{conv.prom}, the condition is converted
to the promoted type.
Any
statement within the \keyword{switch} statement can be labeled with one or
more case labels as follows:
\begin{ncbnf}
\indextext{label!\idxcode{case}}%
\keyword{case} constant-expression \terminal{:}
\end{ncbnf}
where the \grammarterm{constant-expression} shall be
a converted constant expression\iref{expr.const} of the
adjusted type of the switch condition. No two of the case constants in
the same switch shall have the same value after conversion.
\pnum
\indextext{label!\idxcode{default}}%
There shall be at most one label of the form
\begin{codeblock}
default :
\end{codeblock}
within a \keyword{switch} statement.
\pnum
Switch statements can be nested; a \keyword{case} or \keyword{default} label
is associated with the smallest switch enclosing it.
\pnum
When the \keyword{switch} statement is executed, its condition is
evaluated.
\indextext{label!\idxcode{case}}%
If one of the case constants has the same value as the condition,
control is passed to the statement following the matched case label. If
no case constant matches the condition, and if there is a
\indextext{label!\idxcode{default}}%
\keyword{default} label, control passes to the statement labeled by the
default label. If no case matches and if there is no \keyword{default}
then none of the statements in the switch is executed.
\pnum
\keyword{case} and \keyword{default} labels in themselves do not alter the
flow of control, which continues unimpeded across such labels. To exit
from a switch, see \keyword{break}, \ref{stmt.break}.
\begin{note}
Usually, the substatement that is the subject of a switch is compound
and \keyword{case} and \keyword{default} labels appear on the top-level
statements contained within the (compound) substatement, but this is not
required.
\indextext{statement!declaration in \tcode{switch}}%
Declarations can appear in the substatement of a
\keyword{switch} statement.
\end{note}
\pnum
A \keyword{switch} statement of the form
\begin{ncsimplebnf}
\keyword{switch} \terminal{(} init-statement condition \terminal{)} statement
\end{ncsimplebnf}
is equivalent to
\begin{ncsimplebnf}
\terminal{\{}\br
\bnfindent init-statement\br
\bnfindent \keyword{switch} \terminal{(} condition \terminal{)} statement\br
\terminal{\}}
\end{ncsimplebnf}
except that the \grammarterm{init-statement} is in
the same scope as the \grammarterm{condition}.
\indextext{statement!selection|)}
\rSec1[stmt.iter]{Iteration statements}%
\rSec2[stmt.iter.general]{General}%
\indextext{statement!iteration|(}
\pnum
Iteration statements specify looping.
\indextext{statement!\idxcode{while}}%
\indextext{statement!\idxcode{do}}%
\indextext{statement!\idxcode{for}}%
%
\begin{bnf}
\nontermdef{iteration-statement}\br
\keyword{while} \terminal{(} condition \terminal{)} statement\br
\keyword{do} statement \keyword{while} \terminal{(} expression \terminal{)} \terminal{;}\br
\keyword{for} \terminal{(} init-statement \opt{condition} \terminal{;} \opt{expression} \terminal{)} statement\br
\keyword{for} \terminal{(} \opt{init-statement} for-range-declaration \terminal{:} for-range-initializer \terminal{)} statement
\end{bnf}
\begin{bnf}
\nontermdef{for-range-declaration}\br
\opt{attribute-specifier-seq} decl-specifier-seq declarator\br
structured-binding-declaration
\end{bnf}
\begin{bnf}
\nontermdef{for-range-initializer}\br
expr-or-braced-init-list
\end{bnf}
See~\ref{dcl.meaning} for the optional \grammarterm{attribute-specifier-seq} in a
\grammarterm{for-range-declaration}.
\begin{note}
An \grammarterm{init-statement} ends with a semicolon.
\end{note}
\pnum
\indextext{scope!\idxgram{iteration-statement}}%
The substatement in an \grammarterm{iteration-statement} implicitly defines
a block scope\iref{basic.scope} which is entered and exited each time
through the loop.
If the substatement in an \grammarterm{iteration-statement} is
a single statement and not a \grammarterm{compound-statement},
it is as if it was rewritten to be
a \grammarterm{compound-statement} containing the original statement.
\begin{example}
\begin{codeblock}
while (--x >= 0)
int i;
\end{codeblock}
can be equivalently rewritten as
\begin{codeblock}
while (--x >= 0) {
int i;
}
\end{codeblock}
Thus after the \keyword{while} statement, \tcode{i} is no longer in scope.
\end{example}
\pnum
A \defnadj{trivially empty}{iteration statement} is
an iteration statement matching one of the following forms:
\begin{itemize}
\item \tcode{while (} \grammarterm{expression} \tcode{) ;}
\item \tcode{while (} \grammarterm{expression} \tcode{) \{ \}}
\item \tcode{do ; while (} \grammarterm{expression} \tcode{) ;}
\item \tcode{do \{ \} while (} \grammarterm{expression} \tcode{) ;}
\item \tcode{for (} \grammarterm{init-statement} \opt{\grammarterm{expression}} \tcode{; ) ;}
\item \tcode{for (} \grammarterm{init-statement} \opt{\grammarterm{expression}} \tcode{; ) \{ \}}
\end{itemize}
The \defnadj{controlling}{expression} of a trivially empty iteration statement
is the \grammarterm{expression} of
a \tcode{while}, \tcode{do}, or \tcode{for} statement
(or \tcode{true}, if the \tcode{for} statement has no \grammarterm{expression}).
A \defnadj{trivial infinite}{loop} is a trivially empty iteration statement
for which the converted controlling expression is a constant expression,
when interpreted as a \grammarterm{constant-expression}\iref{expr.const}, and
evaluates to \tcode{true}.
The \grammarterm{statement} of a trivial infinite loop is replaced with
a call to the function \tcode{std::this_thread::yield}\iref{thread.thread.this};
it is implementation-defined whether this replacement occurs
on freestanding implementations.
\begin{note}
In a freestanding environment,
concurrent forward progress is not guaranteed;
such systems therefore require explicit cooperation.
A call to yield can add implicit cooperation where none is otherwise intended.
\end{note}
\rSec2[stmt.while]{The \keyword{while} statement}%
\indextext{statement!\idxcode{while}}
\pnum
In the \keyword{while} statement, the substatement is executed repeatedly
until the value of the condition\iref{stmt.pre} becomes
\tcode{false}. The test takes place before each execution of the
substatement.
\pnum
\indextext{statement!declaration in \tcode{while}}%
A \keyword{while} statement is equivalent to
\begin{ncsimplebnf}
\exposid{label} \terminal{:}\br
\terminal{\{}\br
\bnfindent \keyword{if} \terminal{(} condition \terminal{)} \terminal{\{}\br
\bnfindent \bnfindent statement\br
\bnfindent \bnfindent \keyword{goto} \exposid{label} \terminal{;}\br
\bnfindent \terminal{\}}\br
\terminal{\}}
\end{ncsimplebnf}
\begin{note}
The variable created in the condition is destroyed and created with each
iteration of the loop.
\begin{example}
\begin{codeblock}
struct A {
int val;
A(int i) : val(i) { }
~A() { }
operator bool() { return val != 0; }
};
int i = 1;
while (A a = i) {
// ...
i = 0;
}
\end{codeblock}
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.
\end{example}
\end{note}
\rSec2[stmt.do]{The \keyword{do} statement}%
\indextext{statement!\idxcode{do}}
\pnum
The expression is contextually converted to \tcode{bool}\iref{conv};
if that conversion is ill-formed, the program is ill-formed.
\pnum
In the \keyword{do} statement, the substatement is executed repeatedly
until the value of the expression becomes \tcode{false}. The test takes
place after each execution of the statement.
\rSec2[stmt.for]{The \tcode{for} statement}%
\indextext{statement!\idxcode{for}}
\pnum
The \keyword{for} statement
\begin{ncsimplebnf}
\keyword{for} \terminal{(} init-statement \opt{condition} \terminal{;} \opt{expression} \terminal{)} statement
\end{ncsimplebnf}
is equivalent to
\begin{ncsimplebnf}
\terminal{\{}\br
\bnfindent init-statement\br
\bnfindent \keyword{while} \terminal{(} condition \terminal{)} \terminal{\{}\br
\bnfindent\bnfindent statement\br
\bnfindent\bnfindent expression \terminal{;}\br
\bnfindent \terminal{\}}\br
\terminal{\}}
\end{ncsimplebnf}
except that the \grammarterm{init-statement} is
in the same scope as the \grammarterm{condition}, and
except that a
\indextext{statement!\tcode{continue} in \tcode{for}}%
\keyword{continue} in \grammarterm{statement} (not enclosed in another
iteration statement) will execute \grammarterm{expression} before
re-evaluating \grammarterm{condition}.
\begin{note}
Thus the first statement specifies initialization for the loop; the
condition\iref{stmt.pre} specifies a test, sequenced before each
iteration, such that the loop is exited when the condition becomes
\tcode{false}; the expression often specifies incrementing that is
sequenced after each iteration.
\end{note}
\pnum
Either or both of the \grammarterm{condition}
and the \grammarterm{expression} can be omitted.
A missing \grammarterm{condition}
makes the implied \keyword{while} clause
equivalent to \tcode{while(true)}.
\rSec2[stmt.ranged]{The range-based \keyword{for} statement}%
\indextext{statement!range based for@range based \tcode{for}}
\pnum
The range-based \keyword{for} statement
\begin{ncsimplebnf}
\keyword{for} \terminal{(} \opt{init-statement} for-range-declaration \terminal{:} for-range-initializer \terminal{)} statement
\end{ncsimplebnf}
is equivalent to
\begin{ncsimplebnf}
\terminal{\{}\br
\bnfindent \opt{init-statement}\br
\bnfindent \keyword{auto} \terminal{\&\&}\exposid{range} \terminal{=} for-range-initializer \terminal{;}\br
\bnfindent \keyword{auto} \exposid{begin} \terminal{=} \exposid{begin-expr} \terminal{;}\br
\bnfindent \keyword{auto} \exposid{end} \terminal{=} \exposid{end-expr} \terminal{;}\br
\bnfindent \keyword{for} \terminal{(} \terminal{;} \exposid{begin} \terminal{!=} \exposid{end}\terminal{;} \terminal{++}\exposid{begin} \terminal{)} \terminal{\{}\br
\bnfindent\bnfindent for-range-declaration \terminal{=} \terminal{*} \exposid{begin} \terminal{;}\br
\bnfindent\bnfindent statement\br
\bnfindent \terminal{\}}\br
\terminal{\}}
\end{ncsimplebnf}
where
\begin{itemize}
\item
if the \grammarterm{for-range-initializer} is an \grammarterm{expression},
it is regarded as if it were surrounded by parentheses (so that a comma operator
cannot be reinterpreted as delimiting two \grammarterm{init-declarator}{s});
\item \exposid{range}, \exposid{begin}, and \exposid{end} are variables defined for
exposition only; and
\item
\exposid{begin-expr} and \exposid{end-expr} are determined as follows:
\begin{itemize}
\item if the type of \exposid{range} is a reference to an
array type \tcode{R}, \exposid{begin-expr} and \exposid{end-expr} are
\exposid{range} and \exposid{range} \tcode{+} \tcode{N}, respectively,
where \tcode{N} is
the array bound. If \tcode{R} is an array of unknown bound or an array of
incomplete type, the program is ill-formed;
\item if the type of \exposid{range} is a reference to a
class type \tcode{C}, and
searches in the scope of \tcode{C}\iref{class.member.lookup}
for the names \tcode{begin} and \tcode{end}
each find at least one declaration,
\exposid{begin-expr} and \exposid{end-expr} are
\tcode{\exposid{range}.begin()} and \tcode{\exposid{range}.end()},
respectively;
\item otherwise, \exposid{begin-expr} and \exposid{end-expr} are
\tcode{begin(\exposid{range})} and \tcode{end(\exposid{range})}, respectively,
where \tcode{begin} and \tcode{end} undergo
argument-dependent lookup\iref{basic.lookup.argdep}.
\begin{note}
Ordinary unqualified lookup\iref{basic.lookup.unqual} is not
performed.
\end{note}
\end{itemize}
\end{itemize}
\begin{example}
\begin{codeblock}
int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
x *= 2;
\end{codeblock}
\end{example}
\begin{note}
The lifetime of some temporaries in the \grammarterm{for-range-initializer}
is extended to cover the entire loop\iref{class.temporary}.
\end{note}
\begin{example}
\begin{codeblock}
using T = std::list<int>;
const T& f1(const T& t) { return t; }
const T& f2(T t) { return t; }
T g();
void foo() {
for (auto e : f1(g())) {} // OK, lifetime of return value of \tcode{g()} extended
for (auto e : f2(g())) {} // undefined behavior
}
\end{codeblock}
\end{example}
\pnum
In the \grammarterm{decl-specifier-seq} of a \grammarterm{for-range-declaration},
each \grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
or \keyword{constexpr}. The \grammarterm{decl-specifier-seq} shall not define a
class or enumeration.%
\indextext{statement!iteration|)}
\rSec1[stmt.jump]{Jump statements}%
\rSec2[stmt.jump.general]{General}%
\indextext{statement!jump}
\pnum
Jump statements unconditionally transfer control.
\indextext{statement!jump}%
\indextext{statement!\idxcode{break}}%
\indextext{statement!\idxcode{continue}}%
\indextext{return statement@\tcode{return} statement|see{\tcode{return}}}%
\indextext{\idxcode{return}}%
\indextext{statement!\idxcode{goto}}%
%
\begin{bnf}
\nontermdef{jump-statement}\br
\keyword{break} \terminal{;}\br
\keyword{continue} \terminal{;}\br
\keyword{return} \opt{expr-or-braced-init-list} \terminal{;}\br
coroutine-return-statement\br
\keyword{goto} identifier \terminal{;}
\end{bnf}
\pnum
\indextext{local variable!destruction of}%
\indextext{scope!destructor and exit from}%
\begin{note}
On exit from a scope (however accomplished), objects with automatic storage
duration\iref{basic.stc.auto} that have been constructed in that scope are destroyed
in the reverse order of their construction\iref{stmt.dcl}.
For temporaries, see~\ref{class.temporary}.
However, the program can be terminated (by calling
\indextext{\idxcode{exit}}%
\indexlibraryglobal{exit}%
\tcode{std::exit()} or
\indextext{\idxcode{abort}}%
\indexlibraryglobal{abort}%
\tcode{std::abort()}\iref{support.start.term}, for example) without
destroying objects with automatic storage duration.
\end{note}
\begin{note}
A suspension of a coroutine\iref{expr.await} is not considered to be an exit from a scope.
\end{note}
\rSec2[stmt.break]{The \keyword{break} statement}%
\indextext{statement!\idxcode{break}}
\pnum
A \keyword{break} statement shall be enclosed by\iref{stmt.pre}
\indextext{\idxgram{iteration-statement}}%
\indextext{statement!\idxcode{switch}}%
an \grammarterm{iteration-statement}\iref{stmt.iter} or
a \keyword{switch} statement\iref{stmt.switch}.
The \keyword{break} statement causes
termination of the smallest such enclosing statement;
control passes to the statement following the
terminated statement, if any.
\rSec2[stmt.cont]{The \keyword{continue} statement}%
\indextext{statement!\idxcode{continue}}
\pnum
A \keyword{continue}
statement shall be enclosed by\iref{stmt.pre} an
\indextext{\idxgram{iteration-statement}}%
\grammarterm{iteration-statement}\iref{stmt.iter}.
The \keyword{continue} statement
causes control to pass to the loop-continuation portion of the
smallest such enclosing statement, that is, to the end
of the loop. More precisely, in each of the statements
\begin{minipage}{.30\hsize}
\begin{codeblock}
while (foo) {
{
// ...
}
@\exposid{contin}@: ;
}
\end{codeblock}
\end{minipage}
\begin{minipage}{.30\hsize}
\begin{codeblock}
do {
{
// ...
}
@\exposid{contin}@: ;
} while (foo);
\end{codeblock}
\end{minipage}
\begin{minipage}{.30\hsize}
\begin{codeblock}
for (;;) {
{
// ...
}
@\exposid{contin}@: ;
}
\end{codeblock}
\end{minipage}
a \keyword{continue} not contained in an enclosed iteration statement is
equivalent to \tcode{goto} \exposid{contin}.
\rSec2[stmt.return]{The \keyword{return} statement}%
\indextext{\idxcode{return}}%
\indextext{function return|see{\tcode{return}}}%
\pnum
A function returns control to its caller by the \tcode{return} statement.
\pnum
The \grammarterm{expr-or-braced-init-list}
of a \tcode{return} statement is called its operand. A \tcode{return} statement with
no operand shall be used only in a function whose return type is
\cv{}~\keyword{void}, a constructor\iref{class.ctor}, or a
destructor\iref{class.dtor}.
\indextext{\idxcode{return}!constructor and}%
\indextext{\idxcode{return}!constructor and}%
A \tcode{return} statement with an operand of type \keyword{void} shall be used only
in a function that has a \cv{}~\keyword{void} return type.
A \tcode{return} statement with any other operand shall be used only
in a function that has a return type other than \cv{}~\keyword{void};
\indextext{conversion!return type}%
the \tcode{return} statement initializes the
returned reference or prvalue result object
of the (explicit or implicit) function call
by copy-initialization\iref{dcl.init} from the operand.
\begin{note}
A constructor or destructor does not have a return type.
\end{note}
\begin{note}
A \tcode{return} statement can involve
an invocation of a constructor to perform a copy or move of the operand
if it is not a prvalue or if its type differs from the return type of the function.
A copy operation associated with a \tcode{return} statement can be elided or
converted to a move operation if an automatic storage duration variable is returned\iref{class.copy.elision}.
\end{note}
\pnum
The destructor for the result object
is potentially invoked\iref{class.dtor,except.ctor}.
\begin{example}
\begin{codeblock}
class A {
~A() {}
};
A f() { return A(); } // error: destructor of \tcode{A} is private (even though it is never invoked)
\end{codeblock}
\end{example}
\pnum
Flowing off the end of
a constructor,
a destructor, or
a non-coroutine function with a \cv{}~\keyword{void} return type is
equivalent to a \tcode{return} with no operand.
Otherwise, flowing off the end of a function
that is neither \tcode{main}\iref{basic.start.main} nor a coroutine\iref{dcl.fct.def.coroutine}
results in undefined behavior.
\pnum
The copy-initialization of the result of the call is sequenced before the
destruction of temporaries at the end of the full-expression established
by the operand of the \tcode{return} statement, which, in turn, is sequenced
before the destruction of local variables\iref{stmt.jump} of the block
enclosing the \tcode{return} statement.
\pnum
In a function whose return type is a reference,
other than an invented function for \tcode{std::is_convertible}\iref{meta.rel},
a \tcode{return} statement that binds the returned reference to
a temporary expression\iref{class.temporary} is ill-formed.
\begin{example}
\begin{codeblock}
auto&& f1() {
return 42; // ill-formed
}
const double& f2() {
static int x = 42;
return x; // ill-formed
}
auto&& id(auto&& r) {
return static_cast<decltype(r)&&>(r);
}
auto&& f3() {
return id(42); // OK, but probably a bug
}
\end{codeblock}
\end{example}
\rSec2[stmt.return.coroutine]{The \keyword{co_return} statement}%
\indextext{\idxcode{co_return}}%
\indextext{coroutine return|see{\tcode{co_return}}}%
\begin{bnf}
\nontermdef{coroutine-return-statement}\br
\terminal{co_return} \opt{expr-or-braced-init-list} \terminal{;}
\end{bnf}
\pnum
A \keyword{co_return} statement transfers control to
the caller or resumer of a coroutine\iref{dcl.fct.def.coroutine}.
A coroutine shall not enclose
a \tcode{return} statement\iref{stmt.return}.
\begin{note}
For this determination, it is irrelevant whether the \tcode{return} statement
is enclosed by a discarded statement\iref{stmt.if}.
\end{note}
\pnum
The \grammarterm{expr-or-braced-init-list} of a \keyword{co_return} statement is
called its operand.
Let \placeholder{p} be an lvalue naming the coroutine
promise object\iref{dcl.fct.def.coroutine}.
A \keyword{co_return} statement is equivalent to:
\begin{ncsimplebnf}