Difference between revisions of "cpp/language/statements"
m (→Selection statements: -s) |
m (→Control-flow-limited statements: substatements of consteval if stmt is in c++23 not c++20) |
||
(17 intermediate revisions by 9 users not shown) | |||
Line 2: | Line 2: | ||
{{cpp/language/statements/navbar}} | {{cpp/language/statements/navbar}} | ||
− | Statements are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example: | + | ''Statements'' are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example: |
{{source|1= | {{source|1= | ||
− | int main() { | + | int main() |
+ | { | ||
int n = 1; // declaration statement | int n = 1; // declaration statement | ||
n = n + 1; // expression statement | n = n + 1; // expression statement | ||
Line 25: | Line 26: | ||
===Labeled statements=== | ===Labeled statements=== | ||
− | A labeled statement labels a statement for control flow | + | A labeled statement labels a statement for control flow purposes. |
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc| | + | {{sdsc| |
− | {{spar| | + | {{spar|label statement}} |
}} | }} | ||
− | {{sdsc| | + | {{sdsc end}} |
− | {{spar| | + | |
+ | {{par begin}} | ||
+ | {{par|{{spar|label}}|the label applied to the statement (defined below)}} | ||
+ | {{par|{{spar|statement}}|the statement which the label applies to, it can be a labeled statement itself, allowing multiple labels}} | ||
+ | {{par end}} | ||
+ | |||
+ | ====Labels==== | ||
+ | {{spar|label}} is defined as | ||
+ | {{sdsc begin}} | ||
+ | {{sdsc|num=1| | ||
+ | {{spar optional|attr}} {{spar|identifier}} {{ttb|:}} | ||
}} | }} | ||
− | {{sdsc|num= | + | {{sdsc|num=2| |
− | {{spar|attr}}{{ | + | {{spar optional|attr}} {{ttb|case}} {{spar|constexpr}} {{ttb|:}} |
+ | }} | ||
+ | {{sdsc|num=3| | ||
+ | {{spar optional|attr}} {{ttb|default:}} | ||
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
@1@ target for {{rlp|goto}}; | @1@ target for {{rlp|goto}}; | ||
− | @2@ case label in a {{rlp|switch}} statement; | + | @2@ {{c/core|case}} label in a {{rlp|switch}} statement; |
− | @3@ default label in a {{rlp|switch}} statement. | + | @3@ {{c/core|default}} label in a {{rlp|switch}} statement. |
− | An | + | {{rrev|since=c++11| |
+ | An {{rlp|attributes|attribute}} sequence {{spar|attr}} may appear just at the beginning of the label (in which case it applies to the label), or just before any statement itself, in which case it applies to the entire statement. | ||
+ | }} | ||
− | A label | + | A label with an identifier declared inside a function matches all goto statements with the same identifier in that function, in all nested blocks, before and after its own declaration. |
Two labels in a function must not have the same identifier. | Two labels in a function must not have the same identifier. | ||
+ | |||
+ | {{rrev|since=c++23| | ||
+ | Besides being added to a statement, labels can also be used anywhere in [[#Compound statements|compound statements]]. | ||
+ | }} | ||
Labels are not found by {{rlp|unqualified lookup}}: a label can have the same name as any other entity in the program. | Labels are not found by {{rlp|unqualified lookup}}: a label can have the same name as any other entity in the program. | ||
− | {{source| | + | {{source| |
− | void f() { | + | void f() |
+ | { | ||
{ | { | ||
goto label; // label in scope even though declared later | goto label; // label in scope even though declared later | ||
− | label: | + | label: // label can appear at the end of a block standalone since C++23 |
} | } | ||
goto label; // label ignores block scope | goto label; // label ignores block scope | ||
} | } | ||
− | void g() { | + | void g() |
+ | { | ||
goto label; // error: label not in scope in g() | goto label; // error: label not in scope in g() | ||
} | } | ||
}} | }} | ||
+ | |||
+ | ====Control-flow-limited statements==== | ||
+ | The following statements are ''control-flow-limited statements''{{sep}}: | ||
+ | * The {{spar|compound-statement}} of a {{rlp|try|{{c/core|try}} block}}. | ||
+ | * The {{spar|compound-statement}} of a {{rlp|catch|handler}}. | ||
+ | {{rev begin}} | ||
+ | {{rev|since=c++17| | ||
+ | * All substatements of a {{rlp|if#Constexpr if|constexpr {{c/core|if}} statement}}. | ||
+ | }} | ||
+ | {{rev|since=c++23| | ||
+ | * All substatements of a {{rlp|if#Consteval if|consteval {{c/core|if}} statement}}. | ||
+ | }} | ||
+ | {{rev end}} | ||
+ | |||
+ | For each control-flow-limited statement {{tt|S}}: | ||
+ | * All {{c/core|goto}} target labels delcared in {{tt|S}} can only be referred to by statements in {{tt|S}}. | ||
+ | * Each {{c/core|case}} or {{c/core|default}} label appearing within {{tt|S}} can only be associated with a {{rlp|switch|{{c/core|switch}} statement}} within {{tt|S}}. | ||
===Expression statements=== | ===Expression statements=== | ||
Line 69: | Line 108: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc| | + | {{sdsc| |
− | {{spar|attr}}{{ | + | {{spar optional|attr}} {{spar optional|expression}} {{ttb|;}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
{{par begin}} | {{par begin}} | ||
− | {{par | {{spar|attr}}{{mark c++11}} | + | {{par|{{spar|attr}}|{{mark since c++11}} optional sequence of any number of {{rlp|attributes}}}} |
− | {{par | {{spar|expression}} | an | + | {{par|{{spar|expression}}|an {{rlp|expressions|expression}}}} |
{{par end}} | {{par end}} | ||
Most statements in a typical C++ program are expression statements, such as assignments or function calls. | Most statements in a typical C++ program are expression statements, such as assignments or function calls. | ||
− | An expression statement without an expression is called a ''null statement''. It is often used to provide an empty body to a {{rlp|for}} or {{rlp|while}} loop. It can also be used to carry a label in the end of a compound statement. | + | An expression statement without an expression is called a ''null statement''. It is often used to provide an empty body to a {{rlp|for}} or {{rlp|while}} loop. {{rev inl|until=c++23|It can also be used to carry a label in the end of a compound statement.}} |
===Compound statements=== | ===Compound statements=== | ||
Line 87: | Line 126: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc| | + | {{sdsc| |
− | {{spar|attr}}{{ | + | {{spar optional|attr}} {{ttb|{}} {{spar optional|statement...}} {{spar optional|label...}}{{mark since c++23}} {{ttb|}<!---->}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 104: | Line 143: | ||
Each compound statement introduces its own block {{rlp|scope}}; variables declared inside a block are destroyed at the closing brace in reverse order: | Each compound statement introduces its own block {{rlp|scope}}; variables declared inside a block are destroyed at the closing brace in reverse order: | ||
− | {{source| | + | {{source| |
int main() | int main() | ||
{ // start of outer block | { // start of outer block | ||
Line 115: | Line 154: | ||
f >> str; // expression statement | f >> str; // expression statement | ||
} // end of outer block, str is destroyed, f is closed | } // end of outer block, str is destroyed, f is closed | ||
+ | }} | ||
+ | |||
+ | {{rrev|since=c++23| | ||
+ | A [[#Labeled statements|label]] at the end of a compound statement is treated as if it were followed by a null statement. | ||
}} | }} | ||
Line 120: | Line 163: | ||
A selection statement chooses between one of several control flows. | A selection statement chooses between one of several control flows. | ||
− | |||
− | |||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc|num=1| | + | {{sdsc|num=1| |
− | {{spar|attr}}{{mark optional}} {{ttb| | + | {{spar optional|attr}} {{ttb|if constexpr}}{{mark optional}} {{ttb|(}} {{spar optional|init-statement}} {{spar|condition}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
− | {{sdsc|num=2| | + | {{sdsc|num=2| |
− | {{spar|attr}}{{mark optional}} {{ttb| | + | {{spar optional|attr}} {{ttb|if constexpr}}{{mark optional}} {{ttb|(}} {{spar optional|init-statement}} {{spar|condition}} {{ttb|)}} {{spar|statement}} {{ttb|else}} {{spar|statement}} |
}} | }} | ||
− | {{sdsc|num=3| | + | {{sdsc|num=3| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|switch (}} {{spar optional|init-statement}} {{spar|condition}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
− | {{sdsc | + | {{sdsc|num=4|notes={{mark since c++23}}| |
+ | {{spar optional|attr}} {{ttb|if !}}{{mark optional}} {{ttb|consteval}} {{spar|compound-statement}} | ||
}} | }} | ||
− | + | {{sdsc|num=5|notes={{mark since c++23}}| | |
− | + | {{spar optional|attr}} {{ttb|if !}}{{mark optional}} {{ttb|consteval}} {{spar|compound-statement}} {{ttb|else}} {{spar|statement}} | |
− | {{sdsc|num= | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | |||
− | |||
@1@ {{rlp|if}} statement; | @1@ {{rlp|if}} statement; | ||
@2@ {{rlp|if}} statement with an else clause; | @2@ {{rlp|if}} statement with an else clause; | ||
− | @3@ {{rlp|switch}} statement. | + | @3@ {{rlp|switch}} statement; |
+ | @4@ {{rlp|if#Consteval if|consteval if}} statement; | ||
+ | @5@ {{rlp|if#Consteval if|consteval if}} statement with an else clause. | ||
===Iteration statements=== | ===Iteration statements=== | ||
Line 157: | Line 191: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc|num=1| | + | {{sdsc|num=1| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|while (}} {{spar|condition}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
− | {{sdsc|num=2| | + | {{sdsc|num=2| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|do}} {{spar|statement}} {{ttb|while (}} {{spar|expression}} {{ttb|)}} {{ttb|;}} |
}} | }} | ||
− | {{sdsc|num=3| | + | {{sdsc|num=3| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|for (}} {{spar optional|init-statement condition}} {{ttb|;}} {{spar optional|expression}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
− | {{sdsc|num=4|notes={{mark since c++11}}| | + | {{sdsc|num=4|notes={{mark since c++11}}| |
− | {{spar|attr}}{{ | + | {{spar optional|attr}} {{ttb|for (}} {{spar optional|init-statement}}{{mark since c++20}} {{spar|for-range-decl}} {{ttb|:}} {{spar|for-range-init}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 180: | Line 214: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc|num=1| | + | {{sdsc|num=1| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|break;}} |
}} | }} | ||
− | {{sdsc|num=2| | + | {{sdsc|num=2| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|continue;}} |
}} | }} | ||
− | {{sdsc|num=3| | + | {{sdsc|num=3| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|return}} {{spar optional|expression}} {{ttb|;}} |
}} | }} | ||
− | {{sdsc|num=4|notes={{mark since c++11}}| | + | {{sdsc|num=4|notes={{mark since c++11}}| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|return}} {{spar|braced-init-list}} {{ttb|;}} |
}} | }} | ||
− | {{sdsc|num=5| | + | {{sdsc|num=5| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|goto}} {{spar|identifier}} {{ttb|;}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 200: | Line 234: | ||
@2@ {{rlp|continue}} statement; | @2@ {{rlp|continue}} statement; | ||
@3@ {{rlp|return}} statement with an optional expression; | @3@ {{rlp|return}} statement with an optional expression; | ||
− | @4@ {{rlp|return}} statement using | + | @4@ {{rlp|return}} statement using {{rlp|list initialization}}; |
@5@ {{rlp|goto}} statement. | @5@ {{rlp|goto}} statement. | ||
Line 209: | Line 243: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc|num=1| | + | {{sdsc|num=1| |
{{spar|block-declaration}} | {{spar|block-declaration}} | ||
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ | + | @1@ See {{rlp|declarations|Declarations}} and {{rlp|initialization|Initialization}} for details. |
− | === | + | ==={{c/core|try}} blocks=== |
− | A try block catches exceptions thrown when executing other statements. | + | A {{c/core|try}} block catches exceptions thrown when executing other statements. |
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc|num=1| | + | {{sdsc|num=1| |
− | {{spar|attr | + | {{spar optional|attr}} {{ttb|try}} {{spar|compound-statement handler-sequence}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ | + | @1@ See {{rlp|try|{{c/core|try}} block}} for details. |
+ | |||
{{rev begin}} | {{rev begin}} | ||
{{rev|since=tm_ts| | {{rev|since=tm_ts| | ||
===Atomic and synchronized blocks=== | ===Atomic and synchronized blocks=== | ||
− | An atomic and synchronized block provides | + | An atomic and synchronized block provides {{rlp|transactional memory}}. |
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc|num=1|notes={{mark since tm ts}}| | + | {{sdsc|num=1|notes={{mark since tm ts}}| |
{{ttb|synchronized}} {{spar|compound-statement}} | {{ttb|synchronized}} {{spar|compound-statement}} | ||
}} | }} | ||
− | {{sdsc|num=2|notes={{mark since tm ts}}| | + | {{sdsc|num=2|notes={{mark since tm ts}}| |
{{ttb|atomic_noexcept}} {{spar|compound-statement}} | {{ttb|atomic_noexcept}} {{spar|compound-statement}} | ||
}} | }} | ||
− | {{sdsc|num=3|notes={{mark since tm ts}}| | + | {{sdsc|num=3|notes={{mark since tm ts}}| |
{{ttb|atomic_cancel}} {{spar|compound-statement}} | {{ttb|atomic_cancel}} {{spar|compound-statement}} | ||
}} | }} | ||
− | {{sdsc|num=4|notes={{mark since tm ts}}| | + | {{sdsc|num=4|notes={{mark since tm ts}}| |
{{ttb|atomic_commit}} {{spar|compound-statement}} | {{ttb|atomic_commit}} {{spar|compound-statement}} | ||
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ | + | @1@ {{rlp|transactional memory#Synchronized blocks|synchronized block}}, executed in single total order with all synchronized blocks; |
− | @2@ | + | @2@ {{rlp|transactional memory#Atomic blocks|atomic block}} that aborts on exceptions; |
− | @3@ | + | @3@ {{rlp|transactional memory#Atomic blocks|atomic block}} that rolls back on exceptions; |
− | @4@ | + | @4@ {{rlp|transactional memory#Atomic blocks|atomic block}} that commits on exceptions. |
}} | }} | ||
{{rev end}} | {{rev end}} | ||
Line 255: | Line 290: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc see c | c/language/statements | Statements | nomono=true}} | + | {{dsc see c|c/language/statements|Statements|nomono=true}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|es|ja|ru|zh}} | {{langlinks|es|ja|ru|zh}} |
Latest revision as of 18:46, 13 June 2024
Statements are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example:
int main() { int n = 1; // declaration statement n = n + 1; // expression statement std::cout << "n = " << n << '\n'; // expression statement return 0; // return statement }
C++ includes the following types of statements:
Contents |
[edit] Labeled statements
A labeled statement labels a statement for control flow purposes.
label statement | |||||||||
label | - | the label applied to the statement (defined below) |
statement | - | the statement which the label applies to, it can be a labeled statement itself, allowing multiple labels |
[edit] Labels
label is defined as
attr (optional) identifier :
|
(1) | ||||||||
attr (optional) case constexpr :
|
(2) | ||||||||
attr (optional) default:
|
(3) | ||||||||
An attribute sequence attr may appear just at the beginning of the label (in which case it applies to the label), or just before any statement itself, in which case it applies to the entire statement. |
(since C++11) |
A label with an identifier declared inside a function matches all goto statements with the same identifier in that function, in all nested blocks, before and after its own declaration.
Two labels in a function must not have the same identifier.
Besides being added to a statement, labels can also be used anywhere in compound statements. |
(since C++23) |
Labels are not found by unqualified lookup: a label can have the same name as any other entity in the program.
void f() { { goto label; // label in scope even though declared later label: // label can appear at the end of a block standalone since C++23 } goto label; // label ignores block scope } void g() { goto label; // error: label not in scope in g() }
[edit] Control-flow-limited statements
The following statements are control-flow-limited statements :
|
(since C++17) |
|
(since C++23) |
For each control-flow-limited statement S
:
- All goto target labels delcared in
S
can only be referred to by statements inS
. - Each case or default label appearing within
S
can only be associated with a switch statement withinS
.
[edit] Expression statements
An expression statement is an expression followed by a semicolon.
attr (optional) expression (optional) ;
|
|||||||||
attr | - | (since C++11) optional sequence of any number of attributes |
expression | - | an expression |
Most statements in a typical C++ program are expression statements, such as assignments or function calls.
An expression statement without an expression is called a null statement. It is often used to provide an empty body to a for or while loop. It can also be used to carry a label in the end of a compound statement.(until C++23)
[edit] Compound statements
A compound statement or block groups a sequence of statements into a single statement.
attr (optional) { statement... (optional) label... (optional)(since C++23) }
|
|||||||||
When one statement is expected, but multiple statements need to be executed in sequence (for example, in an if statement or a loop), a compound statement may be used:
if (x > 5) // start of if statement { // start of block int n = 1; // declaration statement std::cout << n; // expression statement } // end of block, end of if statement
Each compound statement introduces its own block scope; variables declared inside a block are destroyed at the closing brace in reverse order:
int main() { // start of outer block { // start of inner block std::ofstream f("test.txt"); // declaration statement f << "abc\n"; // expression statement } // end of inner block, f is flushed and closed std::ifstream f("test.txt"); // declaration statement std::string str; // declaration statement f >> str; // expression statement } // end of outer block, str is destroyed, f is closed
A label at the end of a compound statement is treated as if it were followed by a null statement. |
(since C++23) |
[edit] Selection statements
A selection statement chooses between one of several control flows.
attr (optional) if constexpr (optional) ( init-statement (optional) condition ) statement
|
(1) | ||||||||
attr (optional) if constexpr (optional) ( init-statement (optional) condition ) statement else statement
|
(2) | ||||||||
attr (optional) switch ( init-statement (optional) condition ) statement
|
(3) | ||||||||
attr (optional) if ! (optional) consteval compound-statement
|
(4) | (since C++23) | |||||||
attr (optional) if ! (optional) consteval compound-statement else statement
|
(5) | (since C++23) | |||||||
[edit] Iteration statements
An iteration statement repeatedly executes some code.
attr (optional) while ( condition ) statement
|
(1) | ||||||||
attr (optional) do statement while ( expression ) ;
|
(2) | ||||||||
attr (optional) for ( init-statement condition (optional) ; expression (optional) ) statement
|
(3) | ||||||||
attr (optional) for ( init-statement (optional)(since C++20) for-range-decl : for-range-init ) statement
|
(4) | (since C++11) | |||||||
[edit] Jump statements
A jump statement unconditionally transfers control flow.
attr (optional) break;
|
(1) | ||||||||
attr (optional) continue;
|
(2) | ||||||||
attr (optional) return expression (optional) ;
|
(3) | ||||||||
attr (optional) return braced-init-list ;
|
(4) | (since C++11) | |||||||
attr (optional) goto identifier ;
|
(5) | ||||||||
Note: for all jump statements, transfer out of a loop, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of objects with automatic storage duration that are in scope at the point transferred from but not at the point transferred to. If multiple objects were initialized, the order of destruction is the opposite of the order of initialization.
[edit] Declaration statements
A declaration statement introduces one or more identifiers into a block.
block-declaration | (1) | ||||||||
[edit] try blocks
A try block catches exceptions thrown when executing other statements.
attr (optional) try compound-statement handler-sequence
|
(1) | ||||||||
Atomic and synchronized blocksAn atomic and synchronized block provides transactional memory.
1) synchronized block, executed in single total order with all synchronized blocks;
2) atomic block that aborts on exceptions;
3) atomic block that rolls back on exceptions;
4) atomic block that commits on exceptions.
|
(TM TS) |
[edit] See also
C documentation for Statements
|