C++ Switch

A switch statement lets a C++ program choose one path from several fixed values. It is often clearer than a long else if chain when one expression is compared to exact choices.

When To Use switch

Use switch when you want to compare one value, such as an int, char, or enum, against known constant values. It works well for menu choices, command letters, weekdays, simple status codes, and similar fixed options.

A switch is not the right tool for ranges such as score >= 90. For ranges and more complex boolean expressions, use if, else if, and else.

Basic Example

Each case label names one value that can match the expression in the switch. The break statement exits the switch after the matching code runs.

#include <iostream>

int main(void) {
    int day = 3;

    switch (day) {
        case 1:
            std::cout << "Monday" << std::endl;
            break;
        case 2:
            std::cout << "Tuesday" << std::endl;
            break;
        case 3:
            std::cout << "Wednesday" << std::endl;
            break;
        case 4:
            std::cout << "Thursday" << std::endl;
            break;
        case 5:
            std::cout << "Friday" << std::endl;
            break;
        default:
            std::cout << "Weekend or unknown day" << std::endl;
    }

    return 0;
}

Output:

Wednesday

The value of day is 3, so C++ jumps to case 3. It prints Wednesday, then break stops the switch.

Syntax

The expression after switch goes inside parentheses. Each case uses a constant value followed by a colon. The statements for that case come after the colon.

The default label is optional, but it is useful because it handles values that do not match any case. It works like the final else in an if...else chain.

Why break Matters

If a case does not end with break, execution continues into the next case. This is called fall-through. Sometimes fall-through is intentional, but when you are starting out, include break unless you clearly want several cases to share the same code.

Grouping Cases

You can place several case labels before one block of statements. This lets different values produce the same result without repeating code.

#include <iostream>

int main(void) {
    char grade = 'B';

    switch (grade) {
        case 'A':
        case 'B':
            std::cout << "Strong result" << std::endl;
            break;
        case 'C':
            std::cout << "Passing result" << std::endl;
            break;
        case 'D':
        case 'F':
            std::cout << "Needs improvement" << std::endl;
            break;
        default:
            std::cout << "Unknown grade" << std::endl;
    }

    return 0;
}

Output:

Strong result

Here, case 'A' and case 'B' share the same statements. Since grade is 'B', the program prints Strong result and then exits the switch.

Rules To Remember

  • The switch expression is commonly an int, char, or enum value.
  • Each case value must be a constant, not a variable.
  • Use break after a case unless fall-through is intentional.
  • Use default to handle values that do not match any case.
  • Use if...else when you need ranges or complex conditions.

Takeaway: switch is a readable way to choose among exact values; next, loops will show how to repeat code.