TExcelTAD
Home › C++ › C++ Switch

C++ Switch

When you compare one value against many fixed options, a long if/else if chain becomes repetitive. The switch statement expresses the same idea more compactly.

Basic syntax

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    int day = 3;
    switch (day) {
        case 1:
            cout << "Monday\n";
            break;
        case 2:
            cout << "Tuesday\n";
            break;
        case 3:
            cout << "Wednesday\n";
            break;
        default:
            cout << "Some other day\n";
    }
}
Wednesday

How it works:

  1. The expression in switch (...) is evaluated once.
  2. Execution jumps to the case label with a matching value.
  3. It runs until a break (or the end of the switch).
  4. If no case matches, the default label runs (if present).

The switched value must be an integer, a char or an enum — not a string or a double. Case labels must be compile-time constants.

Fall-through

If you forget break, execution "falls through" into the next case. Usually that is a bug, but it can be used on purpose to group cases:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    char letters[] = {'a', 'x', 'E'};
    for (char c : letters) {
        switch (c) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
            case 'A': case 'E': case 'I': case 'O': case 'U':
                cout << c << " is a vowel\n";
                break;
            default:
                cout << c << " is a consonant\n";
        }
    }
}
a is a vowel
x is a consonant
E is a vowel

Here is what an accidental fall-through looks like:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    int level = 1;
    switch (level) {
        case 1:
            cout << "Easy\n";      // no break!
        case 2:
            cout << "Medium\n";
            break;
        case 3:
            cout << "Hard\n";
            break;
    }
}
Easy
Medium

Tip: When fall-through is intentional and a case contains code, mark it with [[fallthrough]]; so readers (and the compiler's -Wimplicit-fallthrough warning) know it is on purpose.

A small calculator

switch works well with char operators:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    double a = 12, b = 4;
    char ops[] = {'+', '-', '*', '/', '%'};
    for (char op : ops) {
        cout << a << " " << op << " " << b << " = ";
        switch (op) {
            case '+': cout << a + b << "\n"; break;
            case '-': cout << a - b << "\n"; break;
            case '*': cout << a * b << "\n"; break;
            case '/': cout << a / b << "\n"; break;
            default:  cout << "unknown operator\n";
        }
    }
}
12 + 4 = 16
12 - 4 = 8
12 * 4 = 48
12 / 4 = 3
12 % 4 = unknown operator

Declaring variables inside a case

If you need a local variable inside one case, wrap the case body in braces. Otherwise the compiler complains that a jump "crosses initialization":

cpp
switch (choice) {
    case 1: {
        int bonus = 10;      // OK: scoped to this block
        cout << bonus;
        break;
    }
    case 2:
        cout << "two";
        break;
}

switch vs if

Use switch when… Use if when…
comparing one integral/enum value to constants testing ranges (x > 10)
there are many discrete options combining several conditions with &&/||
you want the compiler to warn about missing enum cases comparing strings or doubles

Test yourself

What happens when a case has no break?

  1. Compile error
  2. The switch ends
  3. Execution falls through to the next case
  4. The default case runs

Which type can NOT be used in switch (...)?

  1. int
  2. char
  3. enum
  4. std::string

When does the default label run?

  1. Always first
  2. Always last
  3. When no case matches
  4. Only if it is the first label