Skip to content
C++ Better Explained
Go back
C++ Switch Statement: How It Works with Examples
Edit page

C++ Switch Statement: How It Works with Examples

The switch statement is one of the most useful control flow tools in C++. Once you understand it, you will reach for it often — especially when building menus, handling user input, or managing states in a game or simulation.

This article explains how switch works, covers every part of the syntax, shows real examples, and helps you understand when to use switch versus if-else.


What Is a Switch Statement?

A switch statement evaluates an expression once and then compares its value against a list of cases. When a match is found, execution jumps to that case. If no match is found, the optional default case runs.

Basic syntax:

switch (expression) {
    case value1:
        // code to run if expression == value1
        break;
    case value2:
        // code to run if expression == value2
        break;
    default:
        // code to run if no case matches
}

The expression must evaluate to an integer typeint, char, enum, or similar. You cannot use float, double, or std::string in a switch.


Simple Example: Day of the Week

#include <iostream>
using namespace std;

int main() {
    int day = 3;

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

    return 0;
}

Output:

Wednesday

The break Statement

The break statement is critical — it tells the program to exit the switch block after executing a case. Without it, execution falls through into the next case:

int x = 2;

switch (x) {
    case 1:
        cout << "One" << endl;
    case 2:
        cout << "Two" << endl;   // This runs (match)
    case 3:
        cout << "Three" << endl; // This also runs (fall-through!)
    default:
        cout << "Default" << endl; // This runs too!
}

Output (missing break statements):

Two
Three
Default

This is almost always a bug. Always add break unless you have a deliberate reason for fall-through.


The default Case

default catches any value that does not match a named case. It is optional but strongly recommended — without it, unmatched values silently do nothing:

int score = 75;

switch (score / 10) {
    case 10:
    case 9:
        cout << "A" << endl;
        break;
    case 8:
        cout << "B" << endl;
        break;
    case 7:
        cout << "C" << endl;
        break;
    default:
        cout << "F" << endl;
}

Output:

C

Notice cases 10 and 9 share the same block — this is an intentional use of fall-through to handle multiple values with the same code.


Practical Example: Simple Calculator

Switch is ideal for menu-driven programs:

#include <iostream>
using namespace std;

int main() {
    double a = 10, b = 3;
    char op = '+';

    switch (op) {
        case '+':
            cout << a << " + " << b << " = " << a + b << endl;
            break;
        case '-':
            cout << a << " - " << b << " = " << a - b << endl;
            break;
        case '*':
            cout << a << " * " << b << " = " << a * b << endl;
            break;
        case '/':
            if (b != 0)
                cout << a << " / " << b << " = " << a / b << endl;
            else
                cout << "Division by zero" << endl;
            break;
        default:
            cout << "Unknown operator" << endl;
    }

    return 0;
}

Output:

10 + 3 = 13

Switch with Enums

Switch works especially well with enums, making code much more readable than integer comparisons:

#include <iostream>
using namespace std;

enum class Direction { North, South, East, West };

int main() {
    Direction dir = Direction::North;

    switch (dir) {
        case Direction::North:
            cout << "Heading North" << endl;
            break;
        case Direction::South:
            cout << "Heading South" << endl;
            break;
        case Direction::East:
            cout << "Heading East" << endl;
            break;
        case Direction::West:
            cout << "Heading West" << endl;
            break;
    }

    return 0;
}

Using enum class with switch is a powerful pattern in game development and state machines.


Switch vs If-Else: When to Use Each

SituationUse
Comparing one variable against specific constant valuesswitch
Testing ranges (x > 10, x < 5)if-else
Comparing strings or floatsif-else
Complex boolean conditionsif-else
Enum-based state machinesswitch
Menu selectionswitch

A good rule of thumb: if you find yourself writing if (x == 1) ... else if (x == 2) ... else if (x == 3)..., a switch is likely cleaner.


Common Mistakes

Forgetting break: The most common switch mistake. Always add break at the end of each case unless fall-through is intentional.

Using non-integer types: You cannot switch on std::string or float. For strings, use if-else or a std::map.

Declaring variables inside cases: If you need to declare a variable inside a case, wrap the case body in braces:

case 1: {
    int result = 42; // OK — scoped to this block
    cout << result << endl;
    break;
}

Without the braces, declaring a variable in one case and jumping over it in another causes a compiler error.



Edit page
Share this post on:

Previous Post
C++ Structs Explained: How to Use struct with Examples
Next Post
Insertion Sort in C++: How It Works with Full Source Code