Difference between revisions of "cpp/language/statements"
(fixed italicized text that was overlapping with the following text) |
m (Replaced HTML-tags (separators) with new template {{sep}}.) |
||
Line 43: | Line 43: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{spar|identifier}} {{ttb|:}} |
}} | }} | ||
{{sdsc|num=2|1= | {{sdsc|num=2|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|case}} {{spar|constexpr}} {{ttb|:}} |
}} | }} | ||
{{sdsc|num=3|1= | {{sdsc|num=3|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|default:}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 92: | Line 92: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|1= | {{sdsc|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{spar|expression}}{{mark optional}} {{ttb|;}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 110: | Line 110: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|1= | {{sdsc|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|{}} {{spar|statement...}}{{mark optional}} {{spar|label...}}{{mark optional}}{{mark since c++23}} {{ttb|}<!-- -->}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 148: | Line 148: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|if constexpr}}{{mark optional}} {{ttb|(}} {{spar|init-statement}}{{mark optional}} {{spar|condition}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
{{sdsc|num=2|1= | {{sdsc|num=2|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|if constexpr}}{{mark optional}} {{ttb|(}} {{spar|init-statement}}{{mark optional}} {{spar|condition}} {{ttb|)}} {{spar|statement}} {{ttb|else}} {{spar|statement}} |
}} | }} | ||
{{sdsc|num=3|1= | {{sdsc|num=3|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|switch (}} {{spar|init-statement}}{{mark optional}} {{spar|condition}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
{{sdsc|num=4|notes={{mark since c++23}}|1= | {{sdsc|num=4|notes={{mark since c++23}}|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|if !}}{{mark optional}} {{ttb|consteval}} {{spar|compound-statement}} |
}} | }} | ||
{{sdsc|num=5|notes={{mark since c++23}}|1= | {{sdsc|num=5|notes={{mark since c++23}}|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|if !}}{{mark optional}} {{ttb|consteval}} {{spar|compound-statement}} {{ttb|else}} {{spar|statement}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 175: | Line 175: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|while (}} {{spar|condition}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
{{sdsc|num=2|1= | {{sdsc|num=2|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|do}} {{spar|statement}} {{ttb|while (}} {{spar|expression}} {{ttb|)}} {{ttb|;}} |
}} | }} | ||
{{sdsc|num=3|1= | {{sdsc|num=3|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|for (}} {{spar|init-statement condition}}{{mark optional}} {{ttb|;}} {{spar|expression}}{{mark optional}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
{{sdsc|num=4|notes={{mark since c++11}}|1= | {{sdsc|num=4|notes={{mark since c++11}}|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|for (}} {{spar|init-statement}}{{mark optional}}{{mark since c++20}} {{spar|for-range-decl}} {{ttb|:}} {{spar|for-range-init}} {{ttb|)}} {{spar|statement}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 198: | Line 198: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|break;}} |
}} | }} | ||
{{sdsc|num=2|1= | {{sdsc|num=2|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|continue;}} |
}} | }} | ||
{{sdsc|num=3|1= | {{sdsc|num=3|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|return}} {{spar|expression}}{{mark optional}} {{ttb|;}} |
}} | }} | ||
{{sdsc|num=4|notes={{mark since c++11}}|1= | {{sdsc|num=4|notes={{mark since c++11}}|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|return}} {{spar|braced-init-list}} {{ttb|;}} |
}} | }} | ||
{{sdsc|num=5|1= | {{sdsc|num=5|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|goto}} {{spar|identifier}} {{ttb|;}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 238: | Line 238: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|num=1|1= | {{sdsc|num=1|1= | ||
− | {{spar|attr}} | + | {{spar|attr}}{{sep}}{{mark optional}} {{ttb|try}} {{spar|compound-statement handler-sequence}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} |
Revision as of 08:36, 26 January 2023
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 |
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 |
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() }
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)
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) |
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) | |||||||
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) | |||||||
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.
Declaration statements
A declaration statement introduces one or more identifiers into a block.
block-declaration | (1) | ||||||||
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) |
See also
C documentation for Statements
|