Namespaces
Variants
Views
Actions

Statements

From cppreference.com
< cpp‎ | language
Revision as of 12:56, 1 May 2013 by Cubbi (Talk | contribs)

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

Contents

Labels

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

Template:sparam(optional) identifier : Template:sparam (1)
Template:sparam(optional) case Template:sparam : Template:sparam (2)
Template:sparam(optional) default : Template:sparam (3)
1) Target for goto.
2) Case label in a switch statement.
3) Default label in a switch statement.

An attribute sequence Template:sparam 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.

Expression statements

An expression followed by a semicolon is a statement.

Template:sparam(optional) Template:sparam(optional) ; (1)
Template:sparam(C++11) - optional sequence of any number of attributes
Template:sparam - 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.

{ Template:sparam(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 statements

The selection statements choose between one of several flows of control

Template:sparam(optional) if ( Template:sparam ) Template:sparam (1)
Template:sparam(optional) if ( Template:sparam ) Template:sparam else Template:sparam (2)
Template:sparam(optional) switch ( Template:sparam ) Template:sparam (3)
1) if statement
2) if statement with an else clause
3) switch statement

Iteration statements

The iteration statements repeatedly execute some code.

Template:sparam(optional) while ( Template:sparam ) Template:sparam (1)
Template:sparam(optional) do Template:sparam while ( Template:sparam ) ; (2)
Template:sparam(optional) for ( Template:sparam Template:sparam(optional) ; Template:sparam(optional) ) Template:sparam (3)
Template:sparam(optional) for ( Template:sparam : Template:sparam ) Template:sparam (4)
1) while loop
2) do-while loop
3) for loop
4) range for loop

Jump statements

The jump statements unconditionally transfer flow control

Template:sparam(optional) break ; (1)
Template:sparam(optional) continue ; (2)
Template:sparam(optional) return Template:sparam(optional) ; (3)
Template:sparam(optional) return Template:sparam ; (4) (since C++11)
Template:sparam(optional) goto Template:sparam ; (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

Declaration statements introduce one or more identifiers into a block.

Template:sparam ; (1)
1) See Declarations and Initialization for details.

Try block

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

Template:sparam(optional) try Template:sparam Template:sparam (1)
1) See try/catch for details.