Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

C++ Switch

switch compares one value with a list of constant cases. It is often clearer than a long else if chain when a menu choice, a character or an enum decides what to do.

How switch works

#include <iostream>

int main() {
    int day;
    std::cout << "Day number (1-7): ";
    std::cin >> day;
    switch (day) {
        case 1: std::cout << "Monday\n"; break;
        case 2: std::cout << "Tuesday\n"; break;
        case 3: std::cout << "Wednesday\n"; break;
        case 4: std::cout << "Thursday\n"; break;
        case 5: std::cout << "Friday\n"; break;
        case 6:
        case 7: std::cout << "Weekend!\n"; break;
        default: std::cout << "There is no day " << day << "\n";
    }
    return 0;
}
You type
6
Output
Day number (1-7): Weekend!

break ends the switch. Without it the program falls through into the next case, which is useful for cases that share code (6 and 7) but a bug when forgotten. If fall-through is intended after some code, write [[fallthrough]]; to say so; the compiler then does not warn.

switch on characters

#include <iostream>

int main() {
    char op = '*';
    double a = 6, b = 4;
    switch (op) {
        case '+': std::cout << a + b << "\n"; break;
        case '-': std::cout << a - b << "\n"; break;
        case '*': std::cout << a * b << "\n"; break;
        case '/': std::cout << a / b << "\n"; break;
        default:  std::cout << "Unknown operator\n";
    }
    return 0;
}
Output
24

switch on an enum class

Switching on an enum class (see Enums) is very common. With -Wall, the compiler warns when a case is missing, which catches mistakes when a new value is added later.

#include <iostream>

enum class Light { Red, Yellow, Green };

int main() {
    Light l = Light::Yellow;
    switch (l) {
        case Light::Red:    std::cout << "stop\n"; break;
        case Light::Yellow: std::cout << "get ready\n"; break;
        case Light::Green:  std::cout << "go\n"; break;
    }
    return 0;
}
Output
get ready

Limits

Cases must be constants known when compiling, and the value must be a whole number, a character or an enum. A switch cannot test ranges (marks >= 90) or strings; use if for those. A variable declared inside a case needs braces around that case's code.

switch or if?

Use switch when one whole-number, character or enum value is compared with several fixed values: menus, commands, states. The cases line up neatly and the compiler can warn about missing enum values. Use if and else if for ranges, strings, decimals or conditions that combine several variables. When every case only maps a value to another value (a day number to a name), an array or a std::map is often even shorter.

Practice exercises

  1. Write a calculator menu: 1 adds, 2 subtracts, 3 multiplies, 4 divides, anything else prints an error.
  2. Read a letter and print whether it is a vowel, with cases that fall through.
  3. Add Blue to the Light enum without a case and read the warning.