switch and Choosing a Branch Style
switch for discrete values, fall-through dangers, when switch beats if/else, and the conditional operator.
switch: choosing among discrete values
switch (command) {
case 'h':
print_help();
break;
case 'q':
quit = true;
break;
default:
std::cout << "unknown command\n";
break;
}
switch compares one integral or enum value against case labels. It reads better than a long if/else if chain when the tests are all equality against constants.
The fall-through trap (and the one legit use)
Without break, execution falls through into the next case:
case 1:
std::cout << "one";
// NO break โ also prints "two"!
case 2:
std::cout << "two";
break;
GCC warns about accidental fall-through (-Wimplicit-fallthrough with -Wextra). The one legitimate use โ shared code for several cases โ must be an explicit [[fallthrough]]; comment-attribute so reviewers know it is intended. Beginners: always break;.
switch vs if/else โ how to choose
- Equality against a fixed set of constants (
charcommand,intmenu,enum classstate) โswitch. - Ranges, compound logic,
std::stringcomparisons โif/else. - (
switchonstd::stringdoes not work; if/else with==does.)
One-liners: the conditional operator
std::string label = (score >= 50) ? "pass" : "fail";
cond ? a : b is an expression โ useful for initializing a variable from a tiny choice. Anything hairier belongs in a real if.
Coming up
Conditions decide once; loops decide a thousand times. Next module: repetition.