C++ switch statement

The switch statement in C++ is used to select one of many code blocks to be executed based on the value of an expression. It’s typically used when you need to compare a variable against multiple possible values, making your code cleaner and more readable than using multiple if-else statements.

Note :- A switch-statement can alternatively be written as a set of if-statements.

Syntax:


switch (expression) {
    case value1:
        // code to be executed if expression == value1
        break;
    case value2:
        // code to be executed if expression == value2
        break;
    // additional cases as needed
    default:
        // code to be executed if expression doesn't match any case
}

Explanation:

  • expression: This is the value or variable that you want to test.
  • case value: Each case represents a potential match. If the expression matches the value, the corresponding block of code is executed.
  • break: After executing a case, the break statement is used to exit the switch block. If you omit the break, the code will “fall through” to the next case.
  • default: This is an optional block that executes if none of the case values match the expression.

Example:


#include <iostream>
using namespace std;

int main() {
    int day = 2;

        switch (day)
        {
            case 1:
                cout << "Monday"<< endl;
                break;
            case 2:
                cout << "Tuesday"<< endl;
                break;
            case 3:
                cout << "Wednesday"<< endl;
                break;
          
            default:
                cout << "Invalid day"<< endl;
                break;
        }
   return 0;
}

Output:

Tuesday

Example:


#include <iostream>
using namespace std;

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int main() {
    Day today = Friday;  // No need for 'Day.' here

    switch (today) {
        case Monday:
            cout << "Start of the work week!" << endl;
            break;
        case Friday:
            cout << "End of the work week!" << endl;
            break;
        default:
            cout << "It's a regular day." << endl;
            break;
    }

    return 0;
}

Output:

End of the work week!

C++ switch statement – Questions and Answers

Q 1: What is a switch statement?

Ans: A control statement that selects execution paths based on values.

Q 2: What data types are allowed in switch?

Ans: int, char, enum.

Q 3: What is the use of break in switch?

Ans: To stop case execution.

Q 4: What happens without break?

Ans: Fall-through occurs.

Q 5: Is default case mandatory?

Ans: No.

C++ switch statement – Objective Questions (MCQs)

Q1. Which data types are valid in a switch expression?






Q2. What is the purpose of the break statement in a switch block?






Q3. What happens if break is omitted in a switch case?






Q4. What is the output of the following code?

int x = 2;
switch(x) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
default: cout << "Other";
}






Q5. Is the default case mandatory in a switch statement?






Related C++ switch statement Topics