CodeMIRAI
0 XP

switch

2 min read 4 runnable examples Quiz included

When one value decides between several fixed options, switch is clearer than a long chain of else if:

C++
#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;
        default:
            cout << "Another day" << endl;
    }
    return 0;
}

switch jumps to the case that matches the value. break leaves the switch, and default runs when no case matches.

Fall-through

C++
#include <iostream>
using namespace std;

int main() {
    int day = 2;
    switch (day) {
        case 1:
            cout << "Monday" << endl;
        case 2:
            cout << "Tuesday" << endl;
        case 3:
            cout << "Wednesday" << endl;
        default:
            cout << "Another day" << endl;
    }
    return 0;
}

Without break, execution carries on into the cases below the match: this prints Tuesday, Wednesday and Another day. Forgetting a break is the classic switch bug.

Grouping cases

C++
#include <iostream>
using namespace std;

int main() {
    char grade = 'B';
    switch (grade) {
        case 'A':
        case 'B':
            cout << "Great work" << endl;
            break;
        case 'C':
            cout << "Good" << endl;
            break;
        default:
            cout << "Keep practising" << endl;
    }
    return 0;
}

Several labels in a row share one block of code. A char works as a case value, because characters are small integers.

When you leave out a break on purpose, C++17's [[fallthrough]] tells both the reader and the compiler it's deliberate, so the compiler won't warn:

C++
case 1:
    cout << "Starting... ";
    [[fallthrough]];
case 2:
    cout << "running" << endl;
    break;

A calculator

This example's input is 6 * 7:

C++
#include <iostream>
using namespace std;

int main() {
    double a, b;
    char op;
    cin >> a >> op >> b;
    switch (op) {
        case '+': cout << a + b << endl; break;
        case '-': cout << a - b << endl; break;
        case '*': cout << a * b << endl; break;
        case '/':
            if (b == 0) cout << "Can't divide by zero" << endl;
            else cout << a / b << endl;
            break;
        default:
            cout << "Unknown operator " << op << endl;
    }
    return 0;
}

Switching on an enum

An enum class gives a set of named values a type of its own, and pairs naturally with switch:

C++
enum class Level { Beginner, Intermediate, Advanced };

string advice(Level level) {
    switch (level) {
        case Level::Beginner:     return "Start with variables and loops.";
        case Level::Intermediate: return "Build a small project.";
        case Level::Advanced:     return "Learn the standard library well.";
    }
    return "";
}

Compilers can warn when a switch on an enum misses one of its values, so adding a new level later points you to every switch that needs updating.

What switch can't do

  • Case values must be constant integers: int, char, enums. Not double, and not string.
  • No ranges: there's no standard way to write case 90 to 100.

For strings and ranges, use if and else if:

C++
if (score >= 90) cout << "A";
else if (score >= 75) cout << "B";
else cout << "C";
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.