Стандарт C++ 11 (1119564), страница 44
Текст из файла (страница 44)
— end note ]6.41Selection statements[stmt.select]Selection statements choose one of several flows of control.selection-statement:if ( condition ) statementif ( condition ) statement else statementswitch ( condition ) statementcondition:expressionattribute-specifier-seqopt decl-specifier-seq declarator = initializer-clauseattribute-specifier-seqopt decl-specifier-seq declarator braced-init-listSee 8.3 for the optional attribute-specifier-seq in a condition. In Clause 6, the term substatement refers tothe contained statement or statements that appear in the syntax notation. The substatement in a selectionstatement (each substatement, in the else form of the if statement) implicitly defines a block scope (3.3).If the substatement in a selection-statement is a single statement and not a compound-statement, it is as ifit was rewritten to be a compound-statement containing the original substatement. [ Example:if (x)int i;can be equivalently rewritten asif (x) {int i;}Thus after the if statement, i is no longer in scope.
— end example ]2The rules for conditions apply both to selection-statements and to the for and while statements (6.5). Thedeclarator shall not specify a function or an array. If the auto type-specifier appears in the type-specifier-seq,the type of the identifier being declared is deduced from the initializer as described in 7.1.6.4.3A name introduced by a declaration in a condition (either introduced by the type-specifier-seq or the declarator of the condition) is in scope from its point of declaration until the end of the substatements controlledby the condition. If the name is re-declared in the outermost block of a substatement controlled by thecondition, the declaration that re-declares the name is ill-formed.
[ Example:if (int x = f()) {int x;}else {int x;}// ill-formed, redeclaration of x// ill-formed, redeclaration of x— end example ]4The value of a condition that is an initialized declaration in a statement other than a switch statement is thevalue of the declared variable contextually converted to bool (Clause 4). If that conversion is ill-formed, theprogram is ill-formed.
The value of a condition that is an initialized declaration in a switch statement is thevalue of the declared variable if it has integral or enumeration type, or of that variable implicitly convertedto integral or enumeration type otherwise. The value of a condition that is an expression is the value of theexpression, contextually converted to bool for statements other than switch; if that conversion is ill-formed,§ 6.4© ISO/IEC 2011 – All rights reserved131ISO/IEC 14882:2011(E)the program is ill-formed. The value of the condition will be referred to as simply “the condition” where theusage is unambiguous.5If a condition can be syntactically resolved as either an expression or the declaration of a block-scope name,it is interpreted as a declaration.6In the decl-specifier-seq of a condition, each decl-specifier shall be either a type-specifier or constexpr.6.4.11The if statement[stmt.if ]If the condition (6.4) yields true the first substatement is executed.
If the else part of the selectionstatement is present and the condition yields false, the second substatement is executed. In the secondform of if statement (the one including else), if the first substatement is also an if statement then thatinner if statement shall contain an else part.856.4.2The switch statement[stmt.switch]1The switch statement causes control to be transferred to one of several statements depending on the valueof a condition.2The condition shall be of integral type, enumeration type, or of a class type for which a single non-explicitconversion function to integral or enumeration type exists (12.3). If the condition is of class type, thecondition is converted by calling that conversion function, and the result of the conversion is used in place ofthe original condition for the remainder of this section.
Integral promotions are performed. Any statementwithin the switch statement can be labeled with one or more case labels as follows:case constant-expression :where the constant-expression shall be a converted constant expression (5.19) of the promoted type of theswitch condition. No two of the case constants in the same switch shall have the same value after conversionto the promoted type of the switch condition.3There shall be at most one label of the formdefault :within a switch statement.4Switch statements can be nested; a case or default label is associated with the smallest switch enclosingit.5When the switch statement is executed, its condition is evaluated and compared with each case constant.
Ifone of the case constants is equal to the value of the condition, control is passed to the statement followingthe matched case label. If no case constant matches the condition, and if there is a default label, controlpasses to the statement labeled by the default label. If no case matches and if there is no default thennone of the statements in the switch is executed.6case and default labels in themselves do not alter the flow of control, which continues unimpeded acrosssuch labels.
To exit from a switch, see break, 6.6.1. [ Note: Usually, the substatement that is the subjectof a switch is compound and case and default labels appear on the top-level statements contained within85) In other words, the else is associated with the nearest un-elsed if.§ 6.4.2132© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)the (compound) substatement, but this is not required. Declarations can appear in the substatement of aswitch-statement.
— end note ]6.51Iteration statements[stmt.iter]Iteration statements specify looping.iteration-statement:while ( condition ) statementdo statement while ( expression ) ;for ( for-init-statement conditionopt ; expressionopt ) statementfor ( for-range-declaration : for-range-initializer ) statementfor-init-statement:expression-statementsimple-declarationfor-range-declaration:attribute-specifier-seqopt decl-specifier-seq declaratorfor-range-initializer:expressionbraced-init-listSee 8.3 for the optional attribute-specifier-seq in a for-range-declaration.
[ Note: A for-init-statement endswith a semicolon. — end note ]2The substatement in an iteration-statement implicitly defines a block scope (3.3) which is entered and exitedeach time through the loop.If the substatement in an iteration-statement is a single statement and not a compound-statement, it is as ifit was rewritten to be a compound-statement containing the original statement. [ Example:while (--x >= 0)int i;can be equivalently rewritten aswhile (--x >= 0) {int i;}3Thus after the while statement, i is no longer in scope.
— end example ]4[ Note: The requirements on conditions in iteration statements are described in 6.4. — end note ]6.5.1The while statement[stmt.while]1In the while statement the substatement is executed repeatedly until the value of the condition (6.4) becomesfalse. The test takes place before each execution of the substatement.2When the condition of a while statement is a declaration, the scope of the variable that is declared extendsfrom its point of declaration (3.3.2) to the end of the while statement.
A while statement of the formwhile (T t = x) statementis equivalent tolabel:{T t = x;// start of condition scope§ 6.5.1© ISO/IEC 2011 – All rights reserved133ISO/IEC 14882:2011(E)if (t) {statementgoto label;}// end of condition scope}The variable created in a condition is destroyed and created with each iteration of the loop. [ Example: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 succeedsand once for the condition that fails. — end example ]6.5.2The do statement[stmt.do]1The expression is contextually converted to bool (Clause 4); if that conversion is ill-formed, the program isill-formed.2In the do statement the substatement is executed repeatedly until the value of the expression becomes false.The test takes place after each execution of the statement.6.5.31The for statement[stmt.for]The for statementfor ( for-init-statement conditionopt ; expressionopt ) statementis equivalent to{for-init-statementwhile ( condition ) {statementexpression ;}}except that names declared in the for-init-statement are in the same declarative-region as those declared inthe condition, and except that a continue in statement (not enclosed in another iteration statement) willexecute expression before re-evaluating condition.
[ Note: Thus the first statement specifies initialization forthe loop; the condition (6.4) specifies a test, made before each iteration, such that the loop is exited whenthe condition becomes false; the expression often specifies incrementing that is done after each iteration.— end note ]2Either or both of the condition and the expression can be omitted. A missing condition makes the impliedwhile Clause equivalent to while(true).§ 6.5.3134© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)3If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the forstatement.
[ Example:int i = 42;int a[10];for (int i = 0; i < 10; i++)a[i] = i;int j = i;// j = 42— end example ]6.5.41The range-based for statement[stmt.ranged]For a range-based for statement of the formfor ( for-range-declaration : expression ) statementlet range-init be equivalent to the expression surrounded by parentheses86( expression )and for a range-based for statement of the formfor ( for-range-declaration : braced-init-list ) statementlet range-init be equivalent to the braced-init-list. In each case, a range-based for statement is equivalentto{auto && __range = range-init;for ( auto __begin = begin-expr,__end = end-expr;__begin != __end;++__begin ) {for-range-declaration = *__begin;statement}}where __range, __begin, and __end are variables defined for exposition only, and _RangeT is the type ofthe expression, and begin-expr and end-expr are determined as follows:— if _RangeT is an array type, begin-expr and end-expr are __range and __range + __bound, respectively, where __bound is the array bound.
If _RangeT is an array of unknown size or an array ofincomplete type, the program is ill-formed;— if _RangeT is a class type, the unqualified-ids begin and end are looked up in the scope of class _RangeTas if by class member access lookup (3.4.5), and if either (or both) finds at least one declaration, beginexpr and end-expr are __range.begin() and __range.end(), respectively;— otherwise, begin-expr and end-expr are begin(__range) and end(__range), respectively, where beginand end are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup,namespace std is an associated namespace.86) this ensures that a top-level comma operator cannot be reinterpreted as a delimiter between init-declarators in the declaration of __range.§ 6.5.4© ISO/IEC 2011 – All rights reserved135ISO/IEC 14882:2011(E)[ Example:int array[5] = { 1, 2, 3, 4, 5 };for (int& x : array)x *= 2;— end example ]2In the decl-specifier-seq of a for-range-declaration, each decl-specifier shall be either a type-specifier orconstexpr.6.61Jump statements[stmt.jump]Jump statements unconditionally transfer control.jump-statement:break ;continue ;return expressionopt ;return braced-init-list ;goto identifier ;2On exit from a scope (however accomplished), objects with automatic storage duration (3.7.3) that have beenconstructed in that scope are destroyed in the reverse order of their construction.