Topic 6: Selection Structure

If Statements

Selection structure is used when a decision has to be made, based on some condition, before selecting an instruction to execute. The condition tested in the selection structure must be a Boolean expression (evaluating to true or false). The Boolean expression can contain variables, constants, arithmetic operators, comparison operators, and logical operators.

Types of selection structure:

  • Single-alternative selection structure: A set of instructions is executed only if the condition evaluates to true
  • Dual-alternative selection structure: Executes different sets of instructions based on whether the condition evaluates to true or false
Syntax:
  1. if (condition)
  2. one or more statements (true path)
  3. [else
  4. one or more statements (false path)]
  • The else portion in brackets is optional, executed only if the condition is false. It is only used for dual-alternative selection structures
  • If there are more than one statement then it must be entered as a statement block (enclosed in {})
Example 1: one statement in only true path
    1. if (value == 5)
    2. x += value;
Example 2: Multiple statements in the only true path
    1. if (answer == 3)
    2. {
    3. cout << “congratulations” << endl;
    4. score++;
    5. }
Example 3: one statement in each path
    1. if (savings >= 15000)
    2. cout << “buy a car”;
    3. else
    4. cout << “travel by bus”
Example 4: multiple statements in the true path and one statement in the false path
    1. if (x > 2)
    2. {
    3. cout << “yes”;
    4. x = 5;
    5. }
    6. else
    7. cout << “no”;
Example 5: multiple statements in both paths
    1. if(grade >= 50)
    2. {
    3. cout << “congratulations you passed”;
    4. cout << “you will receive a prize”;
    5. }
    6. else
    7. {
    8. cout << “sorry you failed”;
    9. cout << “you will go on probations”;
    10. }

The if-else Chain

if-else chain is a nested if statement occurring in the else clause of the outer if-else. If any condition is true, the corresponding statement is executed and the chain terminates. Final else is only executed if no conditions were true. Serves as a catch-all case. if-else chain provides one selection from many possible alternatives

Syntax:
...
Figure 6.1

switch statement

The switch statement can be used to code a multiple-alternative selection structure. The statement begins with switch keyword followed by a selector expression in parentheses.

The Selector expression can contain any combination of variables, constants, functions, and operators. It must result in a data type that is bool, char, short, int, or long. There are one or more case clauses (as many as necessary) and each case clause represents a different alternative and contains a value followed by a colon.

Syntax:
...
Figure 6.2
Example
  • ...
    Figure 6.3

Value for each case clause can be a literal constant, a named constant, or an expression composed of literal and named constants. The data type of the value should be the same as the selector expression. Each case clause contains one or more statements processed when the selector expression matches that case’s value


Break statement

Break Statement tells the computer to break out of switch at that point; must be the last statement of a case clause. Without a break statement, the computer continues to process instructions in later case clauses. After processing the break statement, the computer processes the next instruction after the switch statement’s closing brace

Default statement

Can also include one default clause; processed if the selector expression does not match any values in case clauses. default clause can appear anywhere, but is usually entered as the last clause. If it is the last clause, a break statement is not needed at its end. Otherwise, a break statement is needed to prevent the computer from processing later case clauses.