Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/statements"

From cppreference.com
< cpp‎ | language
m (note about label lookup (from a recent standard editorial) and link function scope)
(changed the 7 statement categories to singular; punctuation)
Line 15: Line 15:
  
 
There are seven types of statements:
 
There are seven types of statements:
 +
@1@ expression statement;
 +
@2@ compound statement;
 +
@3@ selection statement;
 +
@4@ iteration statement;
 +
@5@ jump statement;
 +
@6@ declaration statement;
 +
@7@ try block statement.
  
@1@ expression statement
+
===Label===
@2@ compound statement
+
@3@ selection statement
+
@4@ iteration statement
+
@5@ jump statement
+
@6@ declaration statement
+
@7@ try block
+
 
+
===Labels===
+
 
Any statement can be ''labeled'', by providing a label followed by a colon before the statement itself:
 
Any statement can be ''labeled'', by providing a label followed by a colon before the statement itself:
  
Line 44: Line 43:
 
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.
 
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.
  
===Expression statements===
+
===Expression statement===
 
An expression followed by a semicolon is a statement.
 
An expression followed by a semicolon is a statement.
  
Line 96: Line 95:
 
}}
 
}}
  
===Selection statements===
+
===Selection statement===
The selection statements choose between one of several flows of control
+
A selection statement chooses between one of several flows of control.
 
+
 
{{sdsc begin}}
 
{{sdsc begin}}
 
{{sdsc|num=1|1=
 
{{sdsc|num=1|1=
Line 115: Line 113:
 
@3@ {{rlp|switch}} statement
 
@3@ {{rlp|switch}} statement
  
===Iteration statements===
+
===Iteration statement===
The iteration statements repeatedly execute some code.
+
An iteration statement repeatedly executes some code.
 
+
 
{{sdsc begin}}
 
{{sdsc begin}}
 
{{sdsc|num=1|1=
 
{{sdsc|num=1|1=
Line 138: Line 135:
 
@4@ {{rlp|range-for|range for}} loop
 
@4@ {{rlp|range-for|range for}} loop
  
===Jump statements===
+
===Jump statement===
The jump statements unconditionally transfer flow control
+
A jump statement unconditionally transfers flow control.
 
+
 
{{sdsc begin}}
 
{{sdsc begin}}
 
{{sdsc|num=1|1=
 
{{sdsc|num=1|1=
Line 168: Line 164:
  
 
===Declaration statement===
 
===Declaration statement===
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|1=
Line 177: Line 172:
 
@1@ See [[cpp/language/declarations|Declarations]] and [[cpp/language/initialization|Initialization]] for details.
 
@1@ See [[cpp/language/declarations|Declarations]] and [[cpp/language/initialization|Initialization]] for details.
  
===Try block===
+
===Try block statement===
The try block statement provides the ability to catch exceptions thrown when executing other statements.
+
A try block statement provides the ability to catch exceptions thrown when executing other statements.
 
+
 
{{sdsc begin}}
 
{{sdsc begin}}
 
{{sdsc|num=1|1=
 
{{sdsc|num=1|1=

Revision as of 05:42, 21 January 2016

 
 
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
}


There are seven types of statements:

1) expression statement;
2) compound statement;
3) selection statement;
4) iteration statement;
5) jump statement;
6) declaration statement;
7) try block statement.

Contents

Label

Any statement can be labeled, by providing a label followed by a colon before the statement itself:

attr(optional) identifier : statement (1)
attr(optional) case constexpr : statement (2)
attr(optional) default : statement (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 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 function scope. Labels are ignored by unqualified lookup: a label can have the same name as any other entity in the program.

Expression statement

An expression followed by a semicolon is a statement.

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 statement

A compound statement or block is a brace-enclosed sequence of statements.

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 (expr) // start of if-statement
{ // start of block
  int n =1; // declaration statement
  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 statment
       f << "hi\n"; // expression statement
    } // end of block, f is flushed and closed by ofstream's destructor
    std::ifstream f("test.txt"); 
    std::string str;
    f >> str; 
}

Selection statement

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

attr(optional) if ( condition ) statement (1)
attr(optional) if ( condition ) statement else statement (2)
attr(optional) switch ( condition ) statement (3)
1) if statement
2) if statement with an else clause
3) switch statement

Iteration statement

An iteration statement repeatedly executes some code.

attr(optional) while ( condition ) statement (1)
attr(optional) do statement while ( expression ) ; (2)
attr(optional) for ( for-init-statement condition(optional) ; expression(optional) ) statement (3)
attr(optional) for ( 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

Jump statement

A jump statement unconditionally transfers flow control.

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.

Declaration statement

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

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

Try block statement

A try block statement provides the ability to catch exceptions thrown when executing other statements.

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

See also

C documentation for Statements