Skip to content
Laviyx Learn

Try

C++ course menu

C++ switch Statement: case, break, default and Fallthrough

Learn the C++ switch statement: matching a value against cases, why break matters, intentional fallthrough with [[fallthrough]], grouping cases, the default case, switching on char and enum class, and when to use if-else instead.

Beginner10 min readUpdated
On this page

When you compare one value against many fixed options, a long else if chain gets repetitive. The switch statement expresses "do something different for each value" more clearly.

Basic switch#

C++
#include <iostream>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            std::cout << "Monday\n";
            break;
        case 2:
            std::cout << "Tuesday\n";
            break;
        case 3:
            std::cout << "Wednesday\n";
            break;
        default:
            std::cout << "Another day\n";
            break;
    }
}
Output
Wednesday

How it works:

  1. The value in switch (...) is evaluated once.
  2. Execution jumps to the case with a matching value.
  3. It runs until a break, which leaves the switch.
  4. If no case matches, default runs (if there is one).

Why break matters: fallthrough#

Without break, execution falls through into the next case and keeps going:

C++
#include <iostream>

int main() {
    int level = 2;

    switch (level) {
        case 1:
            std::cout << "Level 1\n";
        case 2:
            std::cout << "Level 2\n";
        case 3:
            std::cout << "Level 3\n";
        default:
            std::cout << "Done\n";
    }
}
Output
Level 2
Level 3
Done

Forgetting break is the most common switch bug. Compile with -Wextra (-Wimplicit-fallthrough) and the compiler warns you.

Grouping cases#

Several cases can share one block by listing them together. This is intentional fallthrough with no code in between, which is fine:

C++
#include <iostream>

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

    switch (grade) {
        case 'A':
        case 'B':
            std::cout << "Excellent\n";
            break;
        case 'C':
            std::cout << "Good\n";
            break;
        case 'D':
        case 'E':
            std::cout << "Needs improvement\n";
            break;
        default:
            std::cout << "Invalid grade\n";
    }
}
Output
Excellent

If you really want to run one case's code and then continue into the next, mark it with [[fallthrough]]; (C++17). It tells readers and the compiler that the missing break is deliberate.

A menu with switch#

switch is a natural fit for menus and commands:

C++
#include <iostream>

int main() {
    std::cout << "1) Balance  2) Deposit  3) Exit\n";
    std::cout << "Choose: ";
    int choice = 0;
    std::cin >> choice;

    switch (choice) {
        case 1:
            std::cout << "Your balance is 5000\n";
            break;
        case 2:
            std::cout << "How much would you like to deposit?\n";
            break;
        case 3:
            std::cout << "Goodbye\n";
            break;
        default:
            std::cout << "Unknown option\n";
    }
}
Output
1) Balance  2) Deposit  3) Exit
Choose: 2
How much would you like to deposit?

Switching on an enum class#

switch works especially well with an enum class (covered in Structs and Enums). If you leave out a value and have no default, -Wall warns you that a case is not handled:

C++
#include <iostream>

enum class Light { Red, Yellow, Green };

int main() {
    Light light = Light::Yellow;

    switch (light) {
        case Light::Red:
            std::cout << "Stop\n";
            break;
        case Light::Yellow:
            std::cout << "Slow down\n";
            break;
        case Light::Green:
            std::cout << "Go\n";
            break;
    }
}
Output
Slow down

What switch cannot do#

  • The value must be an integer, char or enum type. You cannot switch on a std::string or a double.
  • Each case label must be a constant known at compile time, such as 5 or 'x', not a variable or a range like case 1..10.
  • To declare a variable inside a case, wrap the case's code in braces { }.

For ranges (marks >= 90) or strings, use if / else if.

Common mistakes#

Practice#

Practice exercise

Write a tiny calculator: given double a = 12, b = 4; and char op = '/';, use a switch on op to print the result of +, -, * or /. Print Cannot divide by zero if op is / and b is 0, and Unknown operator for anything else.

Show solution
C++
#include <iostream>

int main() {
    double a = 12;
    double b = 4;
    char op = '/';

    switch (op) {
        case '+':
            std::cout << a + b << '\n';
            break;
        case '-':
            std::cout << a - b << '\n';
            break;
        case '*':
            std::cout << a * b << '\n';
            break;
        case '/':
            if (b == 0) {
                std::cout << "Cannot divide by zero\n";
            } else {
                std::cout << a / b << '\n';
            }
            break;
        default:
            std::cout << "Unknown operator\n";
    }
}
Output
3

Key takeaway#

Finished this lesson?