19.2.1. Statements

[ fromfile: controlstructures.xml id: statements ]

A C++ program contains statements that alter the state of the storage managed by the program and determine the flow of program execution. There are several types of C++ statements, most of which are inherited from the C language. First, there is the simple statement, terminated with a semicolon.

 x = y + z; 

Next, there is the compound statement, or block, consisting of a sequence of statements enclosed in curly braces.

{
    int temp = x;
    x = y;
    y = temp;
}

The preceding example is a single compound statement that contains three simple statements that are executed in sequence, top to bottom. The variable temp is local to the block and is destroyed when the end of the block is reached. A compound statement may contain other compound statements.

In general, a compound statement can be placed wherever a simple statement can go. The reverse is not always true, however. In particular, the function definition

double area(double length, double width) {
    return length * width; 
}

cannot be replaced by

double area(double length, double width)
   return length * width;

The body of a function definition must always be a block.