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.
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#
#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;
}
}WednesdayHow it works:
- The value in
switch (...)is evaluated once. - Execution jumps to the
casewith a matching value. - It runs until a
break, which leaves theswitch. - If no case matches,
defaultruns (if there is one).
Why break matters: fallthrough#
Without break, execution falls through into the next case and keeps going:
#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";
}
}Level 2
Level 3
DoneForgetting 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:
#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";
}
}ExcellentIf 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:
#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";
}
}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:
#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;
}
}Slow downWhat switch cannot do#
- The value must be an integer,
charor enum type. You cannotswitchon astd::stringor adouble. - Each
caselabel must be a constant known at compile time, such as5or'x', not a variable or a range likecase 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
#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";
}
}3Key takeaway#
Finished this lesson?