0
0
CppHow-ToBeginner · 3 min read

How to Use Switch Case in C++: Syntax and Examples

In C++, use the switch statement to select one of many code blocks to execute based on a variable's value. Each case defines a value to match, and break ends the case to prevent fall-through. The default case runs if no other case matches.
📐

Syntax

The switch statement evaluates an expression and executes the matching case block. Use break to stop execution after a case. The default case is optional and runs if no cases match.

  • switch(expression): The value to check.
  • case value: Code to run if expression equals value.
  • break; Stops running more cases.
  • default: Runs if no case matches.
cpp
switch (expression) {
    case value1:
        // code to run if expression == value1
        break;
    case value2:
        // code to run if expression == value2
        break;
    // more cases...
    default:
        // code to run if no case matches
        break;
}
💻

Example

This example shows how to use switch to print a message based on a number from 1 to 3. It demonstrates matching cases and the default case.

cpp
#include <iostream>

int main() {
    int number = 2;

    switch (number) {
        case 1:
            std::cout << "Number is one." << std::endl;
            break;
        case 2:
            std::cout << "Number is two." << std::endl;
            break;
        case 3:
            std::cout << "Number is three." << std::endl;
            break;
        default:
            std::cout << "Number is not 1, 2, or 3." << std::endl;
    }

    return 0;
}
Output
Number is two.
⚠️

Common Pitfalls

Common mistakes include forgetting break statements, which causes "fall-through" where multiple cases run unintentionally. Also, switch only works with integral types like int, char, or enums, not with strings or floating-point numbers.

Example of missing break causing fall-through:

cpp
#include <iostream>

int main() {
    int day = 1;

    switch (day) {
        case 1:
            std::cout << "Monday" << std::endl;
            // missing break here
        case 2:
            std::cout << "Tuesday" << std::endl;
            break;
        default:
            std::cout << "Other day" << std::endl;
    }

    return 0;
}

// Corrected version with break:

/*
switch (day) {
    case 1:
        std::cout << "Monday" << std::endl;
        break;
    case 2:
        std::cout << "Tuesday" << std::endl;
        break;
    default:
        std::cout << "Other day" << std::endl;
}
*/
Output
Monday Tuesday
📊

Quick Reference

  • Use switch for clear multi-choice branching based on integral values.
  • Always add break to prevent fall-through unless intentional.
  • default case is optional but recommended for unexpected values.
  • switch cannot use strings or floating-point types directly.

Key Takeaways

Use switch to select code blocks based on an integral expression's value.
Always include break after each case to avoid running multiple cases unintentionally.
The default case handles values not matched by any case and is optional but useful.
Switch cases only work with integral types like int, char, or enums, not strings or floats.
For multiple values running the same code, stack cases without breaks between them.