Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/statements"

From cppreference.com
< cpp‎ | language
m (Declaration statements: block-declaration contains ; itself)
m (Control-flow-limited statements: substatements of consteval if stmt is in c++23 not c++20)
 
(28 intermediate revisions by 12 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=
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@ expression statements;
+
@1@ labeled statements;
@2@ compound statements;
+
@2@ expression statements;
@3@ selection statements;
+
@3@ compound statements;
@4@ iteration statements;
+
@4@ selection statements;
@5@ jump statements;
+
@5@ iteration statements;
@6@ declaration statements;
+
@6@ jump statements;
@7@ try blocks;
+
@7@ declaration statements;
@8@ atomic and synchronized blocks {{mark since tm ts}}.
+
@8@ try blocks;
 +
@9@ atomic and synchronized blocks {{mark since tm ts}}.
  
===Labels===
+
===Labeled statements===
Any statement can be ''labeled'', by providing a label followed by a colon before the statement itself.
+
A labeled statement labels a statement for control flow purposes.
  
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|
{{spar|attr}}{{mark optional}} {{spar|identifier}} {{ttb|:}} {{spar|statement}}
+
{{spar|label statement}}
 
}}
 
}}
{{sdsc|num=2|1=
+
{{sdsc end}}
{{spar|attr}}{{mark optional}} {{ttb|case}} {{spar|constexpr}} {{ttb|:}} {{spar|statement}}
+
 
 +
{{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=3|1=
+
{{sdsc|num=2|
{{spar|attr}}{{mark optional}} {{ttb|default}} {{ttb|:}} {{spar|statement}}
+
{{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.
 +
 
 +
{{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 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.
 +
 
 +
{{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.
 +
 
 +
{{source|
 +
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()
 +
}
 +
}}
 +
 
 +
====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}}
  
An [[cpp/language/attributes|attribute]] sequence {{spar|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. Labels (and only labels) have {{rlp|scope#Function_scope|function scope}}. Labels are ignored by {{rlp|unqualified lookup}}: a label can have the same name as any other entity in the program.
+
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===
An expression followed by a semicolon is a statement.
+
An expression statement is an expression followed by a semicolon.
  
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|
{{spar|attr}}{{mark optional}} {{spar|expression}}{{mark optional}} {{ttb|;}}
+
{{spar optional|attr}} {{spar optional|expression}} {{ttb|;}}
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
  
 
{{par begin}}
 
{{par begin}}
{{par | {{spar|attr}}{{mark c++11}} | optional sequence of any number of [[cpp/language/attributes|attributes]]}}
+
{{par|{{spar|attr}}|{{mark since c++11}} optional sequence of any number of {{rlp|attributes}}}}
{{par | {{spar|expression}} | an [[cpp/language/expressions|expression]]}}
+
{{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===
Compound statements or ''blocks'' are brace-enclosed sequences of statements.
+
A compound statement or ''block'' groups a sequence of statements into a single statement.
  
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|
{{spar|attr}}{{mark optional}} {{ttb|{}} {{spar|statement...}}{{mark optional}} {{ttb|} }}
+
{{spar optional|attr}} {{ttb|{}} {{spar optional|statement...}} {{spar optional|label...}}{{mark since c++23}} {{ttb|}<!---->}}
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
Line 84: 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|1=
+
{{source|
 
int main()
 
int main()
{
+
{ // start of outer block
     {                                // start of block
+
     {                                // start of inner block
 
         std::ofstream f("test.txt"); // declaration statement
 
         std::ofstream f("test.txt"); // declaration statement
 
         f << "abc\n";                // expression statement
 
         f << "abc\n";                // expression statement
     }                                // end of block: f is flushed and closed
+
     }                                // end of inner block, f is flushed and closed
     std::ifstream f("test.txt");  
+
     std::ifstream f("test.txt"); // declaration statement
     std::string str;
+
     std::string str;             // declaration statement
     f >> str;  
+
     f >> str;                   // expression statement
}
+
} // 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.
 
}}
 
}}
  
 
===Selection statements===
 
===Selection statements===
Selection statements choose between one of several flows of control.
+
A selection statement chooses between one of several control flows.
{{rev begin}}
+
 
{{rev|until=c++17|
+
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|num=1|
{{spar|attr}}{{mark optional}} {{ttb|if}} {{ttb|(}} {{spar|condition}} {{ttb|)}} {{spar|statement}}
+
{{spar optional|attr}} {{ttb|if constexpr}}{{mark optional}} {{ttb|(}} {{spar optional|init-statement}} {{spar|condition}} {{ttb|)}} {{spar|statement}}
 
}}
 
}}
{{sdsc|num=2|1=
+
{{sdsc|num=2|
{{spar|attr}}{{mark optional}} {{ttb|if}} {{ttb|(}} {{spar|condition}} {{ttb|)}} {{spar|statement}} {{ttb|else}} {{spar|statement}}
+
{{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|1=
+
{{sdsc|num=3|
{{spar|attr}}{{mark optional}} {{ttb|switch}} {{ttb|(}} {{spar|condition}} {{ttb|)}} {{spar|statement}}  
+
{{spar optional|attr}} {{ttb|switch (}} {{spar optional|init-statement}} {{spar|condition}} {{ttb|)}} {{spar|statement}}  
 
}}
 
}}
{{sdsc end}}
+
{{sdsc|num=4|notes={{mark since c++23}}|
 +
{{spar optional|attr}} {{ttb|if !}}{{mark optional}} {{ttb|consteval}} {{spar|compound-statement}}
 
}}
 
}}
{{rev|since=c++17|
+
{{sdsc|num=5|notes={{mark since c++23}}|
{{sdsc begin}}
+
{{spar optional|attr}} {{ttb|if !}}{{mark optional}} {{ttb|consteval}} {{spar|compound-statement}} {{ttb|else}} {{spar|statement}}
{{sdsc|num=1|1=
+
{{spar|attr}}{{mark optional}} {{ttb|if}} {{ttb|constexpr}}{{mark optional}} {{ttb|(}} {{spar|init-statement}}{{mark optional}} {{spar|condition}} {{ttb|)}} {{spar|statement}}
+
}}
+
{{sdsc|num=2|1=
+
{{spar|attr}}{{mark optional}} {{ttb|if}} {{ttb|constexpr}}{{mark optional}} {{ttb|(}} {{spar|init-statement}}{{mark optional}} {{spar|condition}} {{ttb|)}} {{spar|statement}} {{ttb|else}} {{spar|statement}}
+
}}
+
{{sdsc|num=3|1=
+
{{spar|attr}}{{mark optional}} {{ttb|switch}} {{ttb|(}} {{spar|init-statement}}{{mark optional}} {{spar|condition}} {{ttb|)}} {{spar|statement}}  
+
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
}}
 
{{rev 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===
Iteration statements repeatedly execute some code.
+
An iteration statement repeatedly executes some code.
  
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|num=1|
{{spar|attr}}{{mark optional}} {{ttb|while}} {{ttb|(}} {{spar|condition}} {{ttb|)}} {{spar|statement}}
+
{{spar optional|attr}} {{ttb|while (}} {{spar|condition}} {{ttb|)}} {{spar|statement}}
 
}}
 
}}
{{sdsc|num=2|1=
+
{{sdsc|num=2|
{{spar|attr}}{{mark optional}} {{ttb|do}} {{spar|statement}} {{ttb|while}} {{ttb|(}} {{spar|expression}} {{ttb|)}} {{ttb|;}}  
+
{{spar optional|attr}} {{ttb|do}} {{spar|statement}} {{ttb|while (}} {{spar|expression}} {{ttb|)}} {{ttb|;}}  
 
}}
 
}}
{{sdsc|num=3|1=
+
{{sdsc|num=3|
{{spar|attr}}{{mark optional}} {{ttb|for}} {{ttb|(}} {{spar|init-statement}} {{spar|condition}}{{mark optional}} {{ttb|;}} {{spar|expression}}{{mark optional}} {{ttb|)}} {{spar|statement}}
+
{{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}}|1=
+
{{sdsc|num=4|notes={{mark since c++11}}|
{{spar|attr}}{{mark optional}} {{ttb|for}} {{ttb|(}} {{spar|for-range-decl}} {{ttb|:}} {{spar|for-range-init}} {{ttb|)}} {{spar|statement}}
+
{{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 156: Line 211:
  
 
===Jump statements===
 
===Jump statements===
Jump statements unconditionally transfer flow control
+
A jump statement unconditionally transfers control flow.
  
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|num=1|
{{spar|attr}}{{mark optional}} {{ttb|break}} {{ttb|;}}
+
{{spar optional|attr}} {{ttb|break;}}
 
}}
 
}}
{{sdsc|num=2|1=
+
{{sdsc|num=2|
{{spar|attr}}{{mark optional}} {{ttb|continue}} {{ttb|;}}
+
{{spar optional|attr}} {{ttb|continue;}}
 
}}
 
}}
{{sdsc|num=3|1=
+
{{sdsc|num=3|
{{spar|attr}}{{mark optional}} {{ttb|return}} {{spar|expression}}{{mark optional}} {{ttb|;}}
+
{{spar optional|attr}} {{ttb|return}} {{spar optional|expression}} {{ttb|;}}
 
}}
 
}}
{{sdsc|num=4|notes={{mark since c++11}}|1=
+
{{sdsc|num=4|notes={{mark since c++11}}|
{{spar|attr}}{{mark optional}} {{ttb|return}} {{spar|braced-init-list}} {{ttb|;}}
+
{{spar optional|attr}} {{ttb|return}} {{spar|braced-init-list}} {{ttb|;}}
 
}}
 
}}
{{sdsc|num=5|1=
+
{{sdsc|num=5|
{{spar|attr}}{{mark optional}} {{ttb|goto}} {{spar|identifier}} {{ttb|;}}
+
{{spar optional|attr}} {{ttb|goto}} {{spar|identifier}} {{ttb|;}}
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
Line 179: 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 [[cpp/language/list_initialization|list initialization]];
+
@4@ {{rlp|return}} statement using {{rlp|list initialization}};
 
@5@ {{rlp|goto}} statement.
 
@5@ {{rlp|goto}} statement.
  
Line 185: Line 240:
  
 
===Declaration statements===
 
===Declaration statements===
Declaration statements introduce one or more identifiers into a block.
+
A declaration statement introduces one or more identifiers into a block.
  
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|num=1|
 
{{spar|block-declaration}}
 
{{spar|block-declaration}}
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
  
@1@ see {{rlp|declarations|Declarations}} and {{rlp|initialization|Initialization}} for details.
+
@1@ See {{rlp|declarations|Declarations}} and {{rlp|initialization|Initialization}} for details.
  
===Try blocks===
+
==={{c/core|try}} blocks===
Try blocks provide the ability to catch 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|1=
+
{{sdsc|num=1|
{{spar|attr}}{{mark optional}} {{ttb|try}} {{spar|compound-statement}} {{spar|handler-sequence}}
+
{{spar optional|attr}} {{ttb|try}} {{spar|compound-statement handler-sequence}}
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
  
@1@ see {{rlp|try_catch|try/catch}} for details.
+
@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===
Atomic and synchronized blocks are used to implement [[cpp/language/transactional_memory|transactional memory]].
+
An atomic and synchronized block provides {{rlp|transactional memory}}.
  
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|notes={{mark since tm ts}}|1=
+
{{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}}|1=
+
{{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}}|1=
+
{{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}}|1=
+
{{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@ [[cpp/language/transactional_memory#Synchronized_blocks|synchronized block]], executed in single total order with all synchronized blocks;
+
@1@ {{rlp|transactional memory#Synchronized blocks|synchronized block}}, executed in single total order with all synchronized blocks;
@2@ [[cpp/language/transactional_memory#Atomic_blocks|atomic block]] that aborts on exceptions;
+
@2@ {{rlp|transactional memory#Atomic blocks|atomic block}} that aborts on exceptions;
@3@ [[cpp/language/transactional_memory#Atomic_blocks|atomic block]] that rolls back on exceptions;
+
@3@ {{rlp|transactional memory#Atomic blocks|atomic block}} that rolls back on exceptions;
@4@ [[cpp/language/transactional_memory#Atomic_blocks|atomic block]] that commits on exceptions.
+
@4@ {{rlp|transactional memory#Atomic blocks|atomic block}} that commits on exceptions.
 
}}
 
}}
 
{{rev end}}
 
{{rev end}}
Line 234: Line 290:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see c | c/language/statements | Statements}}
+
{{dsc see c|c/language/statements|Statements|nomono=true}}
 
{{dsc end}}
 
{{dsc end}}
  
[[zh:cpp/language/statements]]
+
{{langlinks|es|ja|ru|zh}}

Latest revision as of 18:46, 13 June 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

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:

1) labeled statements;
2) expression statements;
3) compound statements;
4) selection statements;
5) iteration statements;
6) jump statements;
7) declaration statements;
8) try blocks;
9) atomic and synchronized blocks (TM TS).

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)
1) target for goto;
2) case label in a switch statement;
3) default label in a switch statement.

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 in S.
  • Each case or default label appearing within S can only be associated with a switch statement within S.

[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)
1) if statement;
2) if statement with an else clause;
3) switch statement;
4) consteval if statement;
5) consteval if statement with an else clause.

[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)
1) while loop;
2) do-while loop;
3) for loop;
4) range for loop.

[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)
1) break statement;
2) continue statement;
3) return statement with an optional expression;
4) return statement using list initialization;
5) goto 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.

[edit] Declaration statements

A declaration statement introduces one or more identifiers into a block.

block-declaration (1)
1) See Declarations and Initialization for details.

[edit] try blocks

A try block catches exceptions thrown when executing other statements.

attr (optional) try compound-statement handler-sequence (1)
1) See try block for details.


Atomic and synchronized blocks

An atomic and synchronized block provides transactional memory.

synchronized compound-statement (1) (TM TS)
atomic_noexcept compound-statement (2) (TM TS)
atomic_cancel compound-statement (3) (TM TS)
atomic_commit compound-statement (4) (TM TS)
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