Examples of switch Statement in C++

Example 1: C++ Program to make a Simple Calculator using the switch

C++
// C Program to create a simpe calculator using switch
// statement
#include <iostream>
#include <stdlib.h>
using namespace std;

// driver code
int main()
{
    // switch variable
    char choice;
    // operands
    int x, y;

    while (1) {
        cout << "Enter the Operator (+,-,*,/)\nEnter x to "
                "exit\n";
        cin >> choice;

        // for exit
        if (choice == 'x') {
            exit(0);
        }

        cout << "Enter the two numbers: ";
        cin >> x >> y;

        // switch case with operation for each operator
        switch (choice) {
        case '+':
            cout << x << " + " << y << " = " << x + y
                 << endl;
            break;

        case '-':
            cout << x << " - " << y << " = " << x - y
                 << endl;
            break;

        case '*':
            cout << x << " * " << y << " = " << x * y
                 << endl;
            break;
        case '/':
            cout << x << " / " << y << " = " << x / y
                 << endl;
            break;
        default:
            printf("Invalid Operator Input\n");
        }
    }
    return 0;
}


Output

Enter the operator (+, -, *, /)

Enter x to exit

+
Enter the two numbers: 100 + 200
100 + 200 = 300

Example 2:  C++ Program to print the day using switch case

C++
// C++ program to returns day based on the numeric value.
#include <iostream>
using namespace std;

class Day {
private:
    int day = 3;

public:
    void set_data()
    {
        cout << "Enter no of day you want to display: ";
        cin >> day;
    }

    void display_day()
    {
        switch (day) {
        case 1:
            cout << "MONDAY";
            break;

        case 2:
            cout << "TUESDAY";
            break;

        case 3:
            cout << "WEDNESDAY";
            break;

        case 4:
            cout << "THURSDAY";
            break;

        case 5:
            cout << "FRIDAY";
            break;

        case 6:
            cout << "SATURDAY";
            break;

        case 7:
            cout << "SUNDAY";
            break;

        default:
            cout << "INVALID INPUT";
            break;
        }
    }
};

main()
{
    Day d1;

    d1.display_day();

    return 0;
}

Output
WEDNESDAY

Example 3:  C++ Program to print different choices using enum values and class

C++
// C++ Program to print different choices using enum values
// and class

#include <iostream>
#include <map>
#include <string>
using namespace std;

// Define an enum class for the choices
enum class Choice { A, B, C };

// Create a map to associate strings with the enum values
map<std::string, Choice> stringToEnumMap
    = { { "A", Choice::A },
        { "B", Choice::B },
        { "C", Choice::C } };

int main()
{
    // The input string
    string x = "A";

    // Convert the string to the corresponding enum using
    // the map
    Choice choice = stringToEnumMap[x];

    // Use a switch statement on the enum
    switch (choice) {
    case Choice::A:
        cout << "Choice is A";
        break;
    case Choice::B:
        cout << "Choice is B";
        break;
    case Choice::C:
        cout << "Choice is C";
        break;
    default:
        cout << "Choice other than A, B and C";
        break;
    }

    return 0;
}

/* The enum class can be used for choice sets
from different projects or programs,
hence improving modularity*/

Output
Choice is A

Switch Statement in C++

The C++ Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. It is an alternative to the long if-else-if ladder which provides an easy way to dispatch execution to different parts of code based on the value of the expression.

Similar Reads

What is a switch statement in C++?

The switch statement in C++ is a flow control statement that is used to execute the different blocks of statements based on the value of the given expression. We can create different cases for different values of the switch expression. We can specify any number of cases in the switch statement but the case value can only be of type int or char....

Syntax of switch Statement in C++

switch (expression) { case value_1: // statements_1; break; case value_2: // statements_2; break; ..... ..... default: // default_statements; break; }...

Example: C Program to demonstrate the syntax of switch in C++

C++ // C++ program to demonstrate syntax of switch #include using namespace std; // Driver Code int main() { // switch variable char x = 'A'; // switch statements switch (x) { case 'A': cout << "Choice is A"; break; case 'B': cout << "Choice is B"; break; case 'C': cout << "Choice is C"; break; default: cout << "Choice other than A, B and C"; break; } return 0; }...

Rules of the switch case statement in C++

There are some rules that we need to follow when using switch statements in C++. They are as follows:...

Flowchart of Switch Statement in C++

Flowchart of the switch statement in C++...

Working of switch Statement in C++

The working of the switch statement in C is as follows:...

Important Points About Switch Case Statements

1. Switch expression should result in a constant value...

Examples of switch Statement in C++

Example 1: C++ Program to make a Simple Calculator using the switch...

Advantages of switch Statement in C++

Easier to debug and maintain for a large number of conditions.Easier to read than if else if....

Disadvantages of switch Statement in C++

Switch case can only evaluate int or char type.No support for logical expressions.Have to keep in mind to add a break in every case....

Conclusion

In this article, we discussed the switch statement and how we can use it to replace long and complex if else if ladder statements. We also discussed the cases in which we can use the switch statement to make our C++ program more efficient....

FAQs on C++ Switch Statement

1. Define switch in C++....