Statements are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example:
intmain(){intn=1;// declaration statementn=n+1;// expression statementstd::cout<<"n = "<<n<<'\n';// expression statementreturn0;// return statement}C++ includes the following types of statements:
(since C++26)
(since C++26)
Labeled statements
A labeled statement labels a statement for control flow purposes.
label statementlabel- 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 Labels
label is defined as
attr(optional)identifier:(1) attr(optional)caseconstexpr:(2) attr(optional)default:(3) 1) target for
(identifier label);
2)case label in a
statement;
3)default label in a
statement.
Two identifier labels in a function must not have the same identifier . identifier s in labels are not found by
: a label can have the same name as any other entity in the program.
case labels and default labels must be
by
statements.
An
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)Besides being added to a statement, labels can also be used anywhere in
.
(since C++23)Identifier labels must not be enclosed by
.
(since C++26)voidf(){label1:1+1;// a labeled statementlabel1:1-1;// Error: cannot repeat label identifiers in the same functionlabel2:// label can appear at the end of a block standalone since C++23}Control-flow-limited statements
The following statements are control-flow-limited statements :
The compound-statement of a
.
The compound-statement of a
.
For each control-flow-limited statement S:
All goto target labels delcared in S can only be referred to by statements in S.
Each case or default label appearing within S can only be associated with a
within S.
Expression statements
An expression statement is an expression followed by a semicolon.
attr(optional)expression(optional);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
or
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
statement or a loop), a compound statement may be used:
if(x>5)// start of if statement{// start of blockintn=1;// declaration statementstd::cout<<n;// expression statement}// end of block, end of if statementEach compound statement introduces its own block
; variables declared inside a block are destroyed at the closing brace in reverse order:
intmain(){// start of outer block{// start of inner blockstd::ofstreamf("test.txt");// declaration statementf<<"abc\n";// expression statement}// end of inner block, f is flushed and closedstd::ifstreamf("test.txt");// declaration statementstd::stringstr;// declaration statementf>>str;// expression statement}// end of outer block, str is destroyed, f is closedA
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
elsestatement(2) attr(optional)switch (init-statement(optional)condition)statement(3) attr(optional)if !(optional)constevalcompound-statement(4) (since C++23)attr(optional)if !(optional)constevalcompound-statementelsestatement(5) (since C++23)1)
statement;
2)
statement with an else clause;
Iteration statements
An iteration statement repeatedly executes some code.
attr(optional)while (condition)statement(1) attr(optional)dostatementwhile (expression);(2) attr(optional)for (init-statement condition(optional);expression(optional))statement(3) attr(optional)for
(init-statement(optional)(since C++20)range-decl:range-init)statement(4) (since C++11)Expansion statements
An expansion statement replaces some code with multiple copies.
attr(optional)template for
(init-statement(optional)expand-decl:expand-init)compound-statement(since C++26)Jump statements
A jump statement unconditionally transfers control flow.
attr(optional)break;(1) attr(optional)continue;(2) attr(optional)returnexpression(optional);(3) attr(optional)returnbraced-init-list;(4) (since C++11)attr(optional)gotoidentifier;(5) 3)
statement with an optional expression;
5)
statement.
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.
Assertion statements
A
assertion.
contract_assertattr(optional)(predicate);(since C++26)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)trycompound-statement handler-sequence(1) Atomic and synchronized blocks
An atomic and synchronized block provides
.
synchronizedcompound-statement(1) (TM TS)atomic_noexceptcompound-statement(2) (TM TS)atomic_cancelcompound-statement(3) (TM TS)atomic_commitcompound-statement(4) (TM TS)1)
, executed in single total order with all synchronized blocks;
(TM TS)Substatements
A substatement of a statement is one of the following:
For a
, its statement .
For a
, any statement of its statement... .
For a
, any of its statement or compound-statement(since C++23).
For an
, its statement .
For an
, its compound-statement .
(since C++26)A statement S1encloses a statement S2 if any of the following conditions is satisfied:
S2 is a substatement of S1
S1 is a selection statement, expansion statement(since C++26) or iteration statement, and S2 is the init-statement of S1.
S1 is a
, and S2 is either its compound-statement or the compound-statement of any
in its handler-seq .
S1 encloses a statement S3 and S3 encloses S2.
A statement S1 is enclosed by a statement S2 if S2 encloses S1.
See also