1. Intro
The switch statement is a multi-way branch that compares a single expression against a list of constant case labels. It is typically used as a cleaner alternative to a long else if ladder when a program needs to compare one variable against many discrete, known values — such as handling a menu choice, a day number, or a grade letter.
Cricket analogy: Choosing switch over a long else if ladder is like a match referee checking a single toss-call value against known outcomes - heads, tails - instead of chaining separate condition checks for each possibility.
2. Syntax
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements executed if no case matches
}3. Explanation
The expression must evaluate to an integral or enumeration type (int, char, enum, etc.) — it cannot be a float, double, or string in standard C++. Each case label must be a constant expression known at compile time. When switch runs, it jumps directly to the case label matching expression's value and begins executing from that point. The optional default label runs when no case matches, and may appear anywhere in the block though it is conventionally placed last.
Cricket analogy: A switch on over_number (an int) works fine like checking which specific over Bumrah is bowling, but you couldn't switch on a float strike rate - just as a bowler can't be selected by a fractional over count.
4. Example
#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 << "Invalid day" << endl;
}
return 0;
}5. Output
Wednesday6. Key Takeaways
switchmatches an integral/char/enum expression against constantcaselabels.- Without a
break, execution falls through into the next case regardless of its label — this is a common bug source. defaultis optional and runs when no case matches; it can appear anywhere but is usually placed last.switchcannot test ranges or floating-point/string values directly — only exact matches on integral constants.
Practice what you learned
1. Which data types can the `switch` expression legally use in standard C++?
2. What happens if a `case` block does not end with `break`?
3. Consider: case 1 has no break and falls into case 2's `cout` statement. If the input matches case 1, which statement(s) execute?
4. What is the role of the `default` label in a switch statement?
5. Which of these can a `case` label NOT be in C++?
Was this page helpful?
You May Also Like
switch vs if-else in C++
Compare C++'s `switch` statement and `if-else`/`else if` ladder to choose the right decision-making construct based on readability, speed, and flexibility.
else if Ladder in C++
Learn how the `else if` ladder chains multiple conditions in C++ to test a series of mutually exclusive cases, stopping at the first true match.
break and continue in C++
Learn the difference between `break`, which exits a loop immediately, and `continue`, which skips to the next iteration, with clear C++ examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics