cpp/language/statements

Statements are fragments of the C++ program that are executed in sequence. The body of any function is a sequence of statements. For example:

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.

Labeled statements
A labeled statement labels a statement for control flow purposes.

is defined as

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

Labels are not found by : a label can have the same name as any other entity in the program.

Expression statements
An expression statement is an expression followed by a semicolon.

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.

Compound statements
A compound statement or block groups a sequence of statements into a single statement.

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:

Each compound statement introduces its own block ; variables declared inside a block are destroyed at the closing brace in reverse order:

Selection statements
A selection statement chooses between one of several control flows.

@1@ statement; @2@ statement with an else clause; @3@ statement; @4@ statement; @5@ statement with an else clause.

Iteration statements
An iteration statement repeatedly executes some code.

@1@ loop; @2@ loop; @3@ loop; @4@ loop.

Jump statements
A jump statement unconditionally transfers control flow.

@1@ statement; @2@ statement; @3@ statement with an optional expression; @4@ statement using ; @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.

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

@1@ see and  for details.

Try blocks
A try block catches exceptions thrown when executing other statements.

@1@ see for details.

{{rev|since=tm_ts|

Atomic and synchronized blocks
An atomic and synchronized block provides.

@1@, executed in single total order with all synchronized blocks; @2@ that aborts on exceptions; @3@ that rolls back on exceptions; @4@ that commits on exceptions. }}