The switch-Statement

  1. Another way to implement multiway branches is to use a switch-statement.

  2. The syntax of the switch-statement is:

    switch (Controlling_Expression)
        {
            case const_1:
                Statement_Sequence_1
            case const_2:
                Statement_Sequence_2
    .
    .
    .
            case const_n:
                Statement_Sequence_n
            default:
                Default_Statement_Sequence
        }

    • The controlling expression is first evaluated. It must return a bool, an integer, a character, or a enum type.
    • The constant values given after the various occurrence of the keyword case is checked one by one until a match is found with the value of the controlling expression.
    • The sequence of statements for that case are then executed.
    • Often each of the statement sequence ends with a break statement. When a break statement is encountered, the switch-statement ends.
    • Execution will fall through to the next case if a break statement is not found at the end of a statement sequence.
    • The default case is optional. The statements associated with it are executed if there is no match with any of the other cases.

  3. //Program to illustrate the switch-statement.
    #include <iostream>
    using namespace std;

    int main( )
    {
        char grade;

        cout << "Enter your midterm grade and press return: ";
        cin >> grade;

        switch (grade)
        {
            case 'A':
                cout << "Excellent. "
                        << "You need not take the final.\n";
                break;
            case 'B':
                cout << "Very good.\n";
                cout << "Good luck on the final exam. "
                break;
            case 'C':
                cout << "You are passing the course.\n";
                break;
            case 'D':
            case 'F':
                cout << "Not good. "
                        << "Go study.\n";
                break;
            default:
                cout << "That is not a possible grade.\n"
                    << "Are you trying to trick me?\n";
        }

        cout << "End of program.\n";
        return 0;
    }

  4. A menu in a computer program presents a list of alternatives on the screen for the use to choose from. The switch-statement is often used for implementing a menu.

  5. Go back to this example after we have dealt with functions.
    //Program to give out homework assignment information.
    #include <iostream>
    using namespace std;

    void show_assignment( );
    //Displays next assignment on screen.

    void show_grade( );
    //Asks for a student number and gives the corresponding grade.

    void give_hints( );
    //Displays a hint for the current assignment.
    int main( )
    {
        int choice;

        do
        {
            cout << endl
                    << "Choose 1 to see the next homework assignment.\n"
                    << "Choose 2 for your grade on the last assignment.\n"
                    << "Choose 3 for assignment hints.\n"
                    << "Choose 4 to exit this program.\n"
                    << "Enter your choice and press return: ";
            cin >> choice;

            switch (choice)
            {
                case 1:
                    show_assignment( );
                    break;
                case 2:
                    show_grade( );
                    break;
                case 3:
                    give_hints( );
                    break;
                case 4:
                    cout << "End of Program.\n";
                    break;
                default:
                    cout << "Not a valid choice.\n"
                        << "Choose again.\n";
            }
        } while (choice != 4);

        return 0;
    }

    void show_assignment( )
    {
        cout << endl << "show_assignment not implemented.\n";
    }

    void show_grade( )
    {
        cout << endl << "show_grade not implemented.\n";
    }

    void give_hints( )
    {
        cout << endl
                << "Assignment hints:\n"
                <<"Analyze the problem\n"
                << "Write an algorithm in pseudocode.\n"
                << "Translate the pseudocode into a C++ program.\n";
    }