C++. The ‘switch’ operator




The ‘switch’ operator


Contents


Search other websites:

1. Assignment of the ‘switch’ selection operator

Depending on the value of the expression, the ‘switch’ selection operator allows you to select one option from several, when solving a problem. Thus, multi-directional branching in the program is provided.

The ‘switch’ statement can be replaced by the ‘if’ statement. However, in some cases, using the ‘switch’ statement can be more efficient than using the ‘if’ statement.

2. The general form of the description of the ‘switch’ statement. Keywords ‘switch’, ‘case’, ‘break’, ‘default’

The general form of writing the ‘switch’ statement is as follows

switch (expression)
{
    case constant1:
        operator_sequence1;
    break;
    case constant2:
        operator_sequence2;
    break;

    ...

    case constantN:
        operator_sequenceN;
    break;
    default
        operator_sequence;
}

where

  • expression – some expression of an integer (int) or character type (char);
  • constant1, constant2, …, constantN – constant values with which the value of the expression is compared. Constant values must be an integer or character type;
  • operator_sequence1, operator_sequence2, …, operator_sequenceN – the corresponding sequence of statements (instructions) that are associated with the corresponding condition;
  • operator_sequencea sequence of statements that is executed if no match is found between the value of the expression and the value of the constants.

The ‘switch’ statement works as follows. The ‘expression’ value is evaluated first. Then this value is compared sequentially with the constants from the given list. Once a match is found for one of the comparison conditions (‘case’ branch), the sequence of instructions that are associated with this comparison is executed. If no matches are found, then the statements that follow after the ‘default’ keyword are executed.

The case keyword is used to specify the value of a constant, when the expression values match, the corresponding sequence of statements will be executed.

The ‘break’ operation aborts the execution of the code, which is specified by the ‘switch’ statement.

The operations that are placed after the ‘default’ keyword are executed if none of the case-constants is the same as the result of evaluating the ‘switch’ expression.

The keyword ‘default’ and the corresponding sequence of statements can be omitted. In this case, if none of the branches follow the word ‘case’, execution is passed to the next operator that follows the ‘switch’ operator.

3. Examples of using the ‘switch’ selection operator

Example 1. The value n = 1..7 is given, which is the day of the week number. By the value of n, determine whether this day is a day off or a work day. The result must be written to the variable fDayOff of type bool.

A snippet of code that solves this problem.

int day;
bool fDayOff;

day = 3;

switch (day)
{
    case 1:
        fDayOff = false;
    break;
    case 2:
        fDayOff = false;
    break;
    case 3:
        fDayOff = false;
    break;
    case 4:
        fDayOff = false;
    break;
    case 5:
        fDayOff = false;
    break;
    case 6:
        fDayOff = true;
    break;
    case 7:
        fDayOff = true;
    break;
}

Another, more compact option for solving this problem.

int day;
bool fDayOff;

day = 7;

switch (day)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        fDayOff = false;
    break;
    case 6:
    case 7:
        fDayOff = true;
    break;
}

Another way to solve this problem

int day;
bool fDayOff;

day = 7;

switch (day)
{
    case 6:
    case 7:
        fDayOff = true;
    break;
    default:
        fDayOff = false;
}

Example 2. Given an integer n = 1..3, which is the number of the function. From the value of variable n, calculate the value of the corresponding function:

1) -2x2-4;    2) 5x+2;    3) 15-3x.

A snippet of code that solves this problem with the abbreviated form of the ‘switch’ statement.

int n;
float f, x;

n = 3;
x = 3;

switch (n)
{
    case 1:
        f = -2*x*x-4;
    break;
    case 2:
        f = 5*x+2;
    break;
    case 3:
        f = 15-3*x;
    break;
}

4. Features of using the break operation in the switch statement

In a switch statement, the break operation is optional. If the ‘break’ operation is present, the ‘switch’ operator exits and executes the next statement. If the ‘break’ operation is present, the ‘switch’ operator exits and following instruction is executed. If there is no break operation in the case branch, then all the statements that are associated with this ‘case’-branch are executed, as well as all the statements that follow immediately after it until another break statement is encountered or the end of the switch is reached.






5. Nested ‘switch’ statements. The general form

The switch statement can have several attachments. In this case, the embedded operator (lower level) ‘switch’ is used as part of the external (top-level) case-sequence of the ‘switch’ operator.

General form of the nested ‘switch’ statement description:

switch (expression)
{
    case constant1:
        operator_sequence1;
    break;
    case constant2:
        operator_sequence2;
    break;

    ...

    case constantK;
        switch (expression)
        {
            ...
        }
    break;

    ...

    case constantN:
        operator_sequenceN;
    break;
    default
        operator_sequence;
}


Related topics