Chapter 5. Statements

5.1 Simple Statements

The expression statement usually end with semicolon. The simplest statement is the empty statement, also known as a null statement.

A compound statement, usually referred to as a block, is a sequence of statements and declarations surrounded by a pair of curly braces. A block is not terminated by a semicolon.

5.2 Statement Scope

Variables defined in the control structure are visible only within that statement and are out of scope after the statement ends.

5.3 Conditional Statements

C++ provides two statements that allow for conditional execution. The if statement determines the flow of control based on a condition. The switch statement evaluates an integral expression and chooses one of several execution paths based on the expression's value.

5.3.1 The if Statement

An if statement conditionally executes an other statement based on whether a specified condition is true. There are two forms of the if: one with an else branch and one without.

The syntactic of the simple if is:

if (condition)
    statement

An if else statement has the form:

if (condition)
    statement
else
    statement2

It is a common mistake to forget the curly braces when multiple statements must be executed as a block.

5.3.2 The switch Statement

A switch statement provides a convenient way of selecting among a number of fixed alternatives.

A switch statement executes by evaluating the parenthesised expression that follows the keyword switch. That expression may be an initialised variable declaration. The expression is converted to integral type. The result of the expression is compared with the value associated with each case. The case keyword and its associated value together are known as the case label.

Forgetting a break is a common source of bugs

It is a common misconception to think that only the statements associated with the matched case label executed.

The default label

The statements following the default label matches the value of the switch expression.

Variable definitions inside the body of a switch

Execution in a switch can jump across case labels. When execution jumps to a particular case, any code that occurred inside the switch before that label is ignored. Therefore, the language does not allow us to jump over an initialisation if the initialise variable is in scope at the point to which control transfers.

5.4 Iterative Statements

5.4.1 The while Statement

A while statement repeatedly executes a target statement as long as a condition is true. Its syntactic form is:

while (condition)
    statement

In a while, statement is executed as long as condition evaluates as true. condition may not be empty. If the first evaluation of condition yields false, statement is not executed.

5.4.2 Traditional for Statement

The syntactic form of the for statement is:

for (initialiser; condition; expression)
    statement

The initialiser is used to initialise or assign a starting value that is modified over the course of the loop. condition serves as the loop control. As long as condition evaluates as true, statement is executed. If the first evaluation of condition yields false, statement is not executed. expression usually modifies the variable initialised in initialiser and tested in condition. expression is evaluated after each iteration of the loop. As usual, statement can be either a single or a compound statement.

5.4.3 Range for Statement

The new standard introduced a simpler for statement that can be used to iterate through the elements of a container or the sequence. The syntactic form of the range for statement is:

for (declaration : expression)
    statement

expression must represent a sequence, such as a braced initialiser list, an array.

5.4.4 The do while Statement

A do while statement is like a while but the condition is tested after the statement body completes. The syntactic form is as follows:

do
    statement
while (condition);

5.5 Jump Statements

Jump statements interrupt the flow of execution. C++ offers four jumps: break, continue, and goto, and the return statement.

5.5.1 The break Statement

A break statement terminates the nearest enclosing while, do while, for, or switch statement.

5.5.2 The continue Statement

A continue statement terminates the current iteration of the nearest enclosing loop and immediately begins the next iteration. A continue can appear only inside a for, while, or do while loop, including inside statements or blocks nested inside such loops.

5.5.3 The goto Statement

A goto statement provides an unconditional jump from the goto to a another statement in the same function.

5.6 try Blocks and Exception Handling

  • throw expressions, which the detecting part uses to indicate that it encountered something it can't handle.

  • try blocks, which the handling part uses to deal with an exception.

  • A set of exception classes that are used to pass information about what happened between a throw and an associated catch.

5.6.1 A throw Expression

The detecting part of a program uses a throw expression to raise an exception. A throw consists of the keyword throw followed by an expression.

5.6.2 The try Block

The general form of a try block is:

try {
    program-statements
} catch (exception-declaration) {
    handler-statements
} catch (exception-declaration) {
    handler-statements
}

5.6.3 Standard Exception

The Standard Exception classes defined in <stdexcept>:

Last updated