
Statements
There are four kinds of statements in C++ : selection, iteration, jump, and expression.

Selection Statements
The if
statement needs, in its simplest form, a logical expression to decide whether to execute the statement following the if statement or not. The example below means that the text will be output if i
is greater than zero.
if (i > 0) { cout << "i is greater then 0"; }
We can also attach an else
part, which is executed if the expression is false.
if (i > 0) { cout << "i is greater then zero"; } else { cout << "i is not greater than zero"; }
Between the if
and else
part we can insert one or more else if
part.
if (i > 0) { cout << "i is greater then zero"; } else if (i == 0) { cout << "i is equal to zero"; } else { cout << "i is less than zero"; }
In the examples above, it is not strictly necessary to surround the output statements with brackets. However, it would be necessary in the case of several statements.In this book, brackets are always used. The brackets and the code in between is called a block.
if (i > 0) { int j = i + 1; cout << "j is " << j; }
A warning may be in order. In an if
statement, it is perfectly legal to use one equals sign instead of two when comparing two values. As one equals sign is used for assignment, not comparison, the variable i
in the following code will be assigned the value one, and the expression will always be true.
if (i = 1) // Always true. { // ... }
One way to avoid the mistake is to swap the variable and the value. As a value can be compared but not assigned, the compiler will issue an error message if you by mistake enter one equals sign instead of two signs.
if (1 = i) // Compile-time error. { // ... }
The switch
statement is simpler than the if
statement, and not as powerful. It evaluates the switch
value and jumps to a case
statement with the same value. If no value matches, it jumps to the default
statement, if present. It is important to remember the break
statement. Otherwise, the execution would simply continue with the code attached to the next case
statement. The break
statement is used to jump out of a switch
or iteration statement. The switch
expression must have an integral or pointer type and two case
statements cannot have the same value. The default
statement can be omitted, and we can only have one default
alternative. However, it must not be placed at the end of the switch
statement, even though it is considered good practice to do so.
switch (i) { case 1: cout << "i is equal to 1" << endl; break; case 2: cout << "i is equal to 2" << endl; break; case 3: cout << "i is equal to 3" << endl; int j = i + 1; cout << "j = " << j; break; default: cout << "i is not equal to 1, 2, or 3." << endl; break; }
In the code above, there will be a warning for the introduction of the variable j
. As a variable is valid only in its closest surrounding scope, the following code below will work without the warning.
switch (i) { // ... case 3: cout << "i is equal to 3" << endl; { int j = i + 1; cout << "j = " << j; } break; // ... }
We can use the fact that an omitted break statement makes the execution continue with the next statement to group several case statements together.
switch (i) { case 1: case 2: case 3: cout << "i is equal to 1, 2, or 3" << endl; break; // ... }
Iteration Statements
Iteration statements iterate one statement (or several statements inside a block) as long as certain condition is true. The simplest iteration statement is the while
statement. It repeats the statement as long as the given expression is true. The example below writes the numbers 1 to 10.
int i = 1; while (i <= 10) { cout << i; ++i; }
The same thing can be done with a do-while
statement.
int i = 1; do { cout << i; ++i; } while (i <= 10);
The do-while
statement is less powerful. If the expression is false at the beginning, the while
statement just skips the repetitions altogether, but the do-while
statement must always execute the repetition statement at least once in order to reach the continuation condition.
We can also use the for
statement, which is a more compact variant of the while statement. It takes three expressions, separated by semicolons. In the code below, the first expression initializes the variable, the repetition continues as long as the second expression is true, and the third expression is executed at the end of each repetition.
for (int i = 1; i <= 10; ++i) { cout << i; }
Similar to the switch
statement, the iteration statements can be interrupted by the break
statement.
int i = 1; while (true) { cout << i; ++i; if (i > 10) { break; } }
Another way to construct an eternal loop is to omit the second expression of a for
statement.
for (int i = 1; ; ++i) { cout << i; if (i > 10) { break; } }
An iteration statement can also include a continue
statement. It skips the rest of the current repetition. The following example writes the numbers 1 to 10 with the exception of 5
.
for (int i = 1; i <= 10; ++i) { if (i == 5) { continue; } cout << i; }
The following example, however, will not work. Because the continue
statement will skip the rest of the while
block, i
will never be updated, and we will be stuck in an infinite loop. Therefore, I suggest you use the continue
statement with care.
int i = 1; while (i <= 10) { if (i == 5) { continue; } cout << i; ++i; }
Jump Statements
We can jump from one location to another inside the same function block by marking the latter location with a label
inside the block with the goto
statement.
int i = 1; label: cout << i; ++ i; if (i <= 10) { goto label; }
The goto
statement is, however, considered to give rise to unstructured code, so called "spaghetti code". I strongly recommend that you avoid the goto
statement altogether.
Expression Statements
An expression can form a statement.
a = b + 1; // Assignment operator. cout << "Hello, World!"; // Stream operator. WriteNumber(5); // Function call.
In the above examples, we are only interested in the side effects; that a
is assigned a new value or that a text or a number is written. We are allowed to write expression statements without side effects; even though it has no meaning and it will probably be erased by the compiler.
a + b * c;