Switch statement in C++ - Time & Space Complexity
We want to understand how the time it takes to run a switch statement changes as the number of cases grows.
Specifically, does adding more cases make the program slower, and by how much?
Analyze the time complexity of the following code snippet.
int exampleSwitch(int x) {
switch (x) {
case 1:
return 10;
case 2:
return 20;
case 3:
return 30;
default:
return 0;
}
}
This code returns a value based on the input using a switch statement with a few cases.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch statement performs a single jump or lookup based on the input value.
- How many times: Executes in constant time; no loops or repeated checks.
Compilers typically optimize switch statements on integers to a jump table (dense cases) or binary search (sparse cases), keeping operations roughly constant.
| Input Size (number of cases) | Approx. Operations |
|---|---|
| 3 | 1 jump/lookup |
| 10 | 1 jump/lookup |
| 100 | 1 jump/lookup (or O(log n) worst case) |
Pattern observation: Time remains constant (O(1)) or logarithmic at worst, independent of case count.
Time Complexity: O(1)
This means the time to find the matching case is constant, regardless of the number of cases (thanks to compiler optimizations like jump tables).
[X] Wrong: "Switch statements check cases one by one, so O(n) time."
[OK] Correct: C++ compilers optimize switches to jump tables (O(1)) for dense integers or binary search (O(log n)), not linear scans.
Understanding switch optimizations shows knowledge of compiler behavior and real-world efficiency beyond naive analysis.
"What if the switch statement was replaced by a hash map lookup? How would the time complexity change?"
Practice
switch statement in C++?Solution
Step 1: Understand the role of switch
Aswitchstatement checks one value and runs code matching that value.Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and variables store data.Final Answer:
To choose which block of code to run based on a single value -> Option DQuick Check:
Switch selects code by value = A [OK]
- Confusing switch with loops
- Thinking switch defines functions
- Mixing switch with variable declaration
choice?Solution
Step 1: Recall switch syntax
The switch statement requires parentheses around the variable:switch(variable).Step 2: Check each option
switch(choice) { uses correct parentheses and opening brace. Others miss parentheses or use wrong punctuation.Final Answer:
switch(choice) { -> Option AQuick Check:
Correct switch syntax uses parentheses [OK]
- Omitting parentheses
- Using semicolon instead of brace
- Adding parentheses after variable name
int x = 2;
switch(x) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
default:
std::cout << "Other";
}Solution
Step 1: Check the value of x
The variablexis 2.Step 2: Match case in switch
The switch matchescase 2, so it prints "Two" and then breaks.Final Answer:
Two -> Option BQuick Check:
Value 2 matches case 2 = Two [OK]
- Forgetting break causes fall-through
- Choosing default when case matches
- Confusing output with case number
int n = 3;
switch(n) {
case 1:
std::cout << "One";
case 2:
std::cout << "Two";
break;
default:
std::cout << "Default";
}Solution
Step 1: Check case 1 block
There is nobreak;aftercase 1, so execution falls through to case 2.Step 2: Understand consequences
Without break, both "One" and "Two" print if n=1. This is usually unintended.Final Answer:
Missing break after case 1 causes fall-through -> Option CQuick Check:
Break prevents fall-through = A [OK]
- Forgetting break causes multiple outputs
- Thinking default must be last (it can be anywhere)
- Believing case labels must be strings
Solution
Step 1: Group cases for weekdays and weekends
Cases 1 to 5 print "Weekday"; cases 6 and 7 print "Weekend". Grouping cases with no code between them is correct.Step 2: Check breaks and outputs
switch(day) { case 1: case 2: case 3: case 4: case 5: std::cout << "Weekday"; break; case 6: case 7: std::cout << "Weekend"; break; default: std::cout << "Invalid"; } groups cases correctly and uses breaks to prevent fall-through. Others mix outputs or miss breaks.Final Answer:
Option A code correctly prints "Weekday" for 1-5 and "Weekend" for 6-7 -> Option AQuick Check:
Group cases + breaks = correct switch [OK]
- Missing breaks causing fall-through
- Swapping outputs for cases
- Not grouping cases properly
Ready to prove it?
Pass the test and this pattern moves unknown
