Difference between revisions of "cpp/language/statements"
m (punctuation) |
(improve statement definitions) |
||
Line 11: | Line 11: | ||
std::cout << "n = " << n << '\n'; // expression statement | std::cout << "n = " << n << '\n'; // expression statement | ||
return 0; // return statement | return 0; // return statement | ||
− | } | + | } |
}} | }} | ||
C++ includes the following types of statements: | C++ includes the following types of statements: | ||
− | @1@ | + | @1@ labeled statements; |
− | @2@ | + | @2@ expression statements; |
− | @3@ | + | @3@ compound statements; |
− | @4@ | + | @4@ selection statements; |
− | @5@ | + | @5@ iteration statements; |
− | @6@ | + | @6@ jump statements; |
− | @7@ try blocks; | + | @7@ declaration statements; |
− | @ | + | @8@ try blocks; |
+ | @9@ atomic and synchronized blocks {{mark since tm ts}}. | ||
− | === | + | ===Labeled statements=== |
− | + | A labeled statement labels a statement for control flow purpose. | |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 54: | Line 55: | ||
void f() | void f() | ||
{ | { | ||
− | { | + | { |
goto label; // label in scope even though declared later | goto label; // label in scope even though declared later | ||
label:; | label:; | ||
Line 68: | Line 69: | ||
===Expression statements=== | ===Expression statements=== | ||
− | An expression followed by a semicolon | + | An expression statement is an expression followed by a semicolon. |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 86: | Line 87: | ||
===Compound statements=== | ===Compound statements=== | ||
− | + | A compound statement or ''block'' groups a sequence of statements into a single statement. | |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 113: | Line 114: | ||
f << "abc\n"; // expression statement | f << "abc\n"; // expression statement | ||
} // end of block: f is flushed and closed | } // end of block: f is flushed and closed | ||
− | std::ifstream f("test.txt"); | + | std::ifstream f("test.txt"); |
std::string str; | std::string str; | ||
− | f >> str; | + | f >> str; |
} | } | ||
}} | }} | ||
===Selection statements=== | ===Selection statements=== | ||
− | + | A selection statements chooses between one of several control flows. | |
+ | |||
{{rev begin}} | {{rev begin}} | ||
{{rev|until=c++17| | {{rev|until=c++17| | ||
Line 155: | Line 157: | ||
===Iteration statements=== | ===Iteration statements=== | ||
− | + | An iteration statement repeatedly executes some code. | |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 178: | Line 180: | ||
===Jump statements=== | ===Jump statements=== | ||
− | + | A jump statement unconditionally transfers control flow. | |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 207: | Line 209: | ||
===Declaration statements=== | ===Declaration statements=== | ||
− | + | A declaration statement introduces one or more identifiers into a block. | |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 218: | Line 220: | ||
===Try blocks=== | ===Try blocks=== | ||
− | + | A try block catches exceptions thrown when executing other statements. | |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 231: | Line 233: | ||
{{rev|since=tm_ts| | {{rev|since=tm_ts| | ||
===Atomic and synchronized blocks=== | ===Atomic and synchronized blocks=== | ||
− | + | An atomic and synchronized block provides [[cpp/language/transactional_memory|transactional memory]]. | |
{{sdsc begin}} | {{sdsc begin}} |
Revision as of 07:46, 4 September 2021
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 purpose.
attr(optional) identifier : statement
|
(1) | ||||||||
attr(optional) case constexpr : statement
|
(2) | ||||||||
attr(optional) default : statement
|
(3) | ||||||||
An attribute sequence attr may appear just before 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 statement may carry multiple labels.
A label (and only a label) with an identifier declared inside a function is in scope everywhere in that function, in all nested blocks, before and after its own declaration.
Two labels in a function must not have the same identifier.
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:; } 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) ;
|
(1) | ||||||||
attr(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.
Compound statements
A compound statement or block groups a sequence of statements into a single statement.
attr(optional) { statement...(optional) }
|
(1) | ||||||||
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 block std::ofstream f("test.txt"); // declaration statement f << "abc\n"; // expression statement } // end of block: f is flushed and closed std::ifstream f("test.txt"); std::string str; f >> str; }
Selection statements
A selection statements chooses between one of several control flows.
|
(until C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||
|
(since C++17) |
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 ( 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
|