Switch statement - Time & Space Complexity
We want to see how the time to run a switch statement changes as the input changes.
Does it take longer if there are more cases or if the input is different?
Analyze the time complexity of the following code snippet.
int example(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 number using a switch statement.
Look for repeated work inside the switch.
- Primary operation: The switch checks the input once to find the matching case.
- How many times: Exactly one time per function call, no loops or repeated checks.
The switch picks the right case quickly without repeating work.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 1 check |
| 10 | 1 check |
| 100 | 1 check |
Pattern observation: The number of operations stays about the same no matter the input size.
Time Complexity: O(1)
This means the time to run the switch does not grow with input size; it stays constant.
[X] Wrong: "A switch statement checks every case one by one, so it takes longer with more cases."
[OK] Correct: Modern compilers often optimize switches to jump directly to the matching case, so it does not check all cases one by one.
Understanding how switch statements work helps you explain code efficiency clearly and shows you know how simple decisions affect performance.
"What if the switch had many cases but no breaks, causing fall-through? How would the time complexity change?"
Practice
switch statement in C?Solution
Step 1: Understand the role of switch
A switch statement chooses one path among many based on a variable's value.Step 2: Compare with other options
Loops repeat code, functions define reusable blocks, and declarations create variables, which are different from switch's purpose.Final Answer:
To select one action from multiple choices based on a variable's value -> Option CQuick Check:
Switch = Choice selection [OK]
- Confusing switch with loops
- Thinking switch defines functions
- Believing switch declares variables
Solution
Step 1: Recall switch syntax
The correct syntax uses parentheses around the variable: switch(variable) { ... }Step 2: Check options
Only switch (variable) { case 1: ... } uses parentheses correctly; others miss parentheses or use wrong brackets.Final Answer:
switch (variable) { case 1: ... } -> Option AQuick Check:
Switch needs parentheses around variable [OK]
- Omitting parentheses around variable
- Using square brackets instead of parentheses
- Using colon instead of braces
int x = 2;
switch (x) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Other\n");
}Solution
Step 1: Identify the switch variable value
The variable x is 2, so the switch checks case 2.Step 2: Match case and output
Case 2 matches, so it prints "Two" and then breaks to stop further checks.Final Answer:
Two -> Option DQuick Check:
Case 2 matches x=2, prints "Two" [OK]
- Forgetting break causes fall-through
- Choosing default when case matches
- Confusing case numbers
int n = 3;
switch (n) {
case 1:
printf("One\n");
case 2:
printf("Two\n");
break;
default:
printf("Default\n");
}Solution
Step 1: Check each case for break
Case 1 lacks a break, so if matched, it would fall through to case 2.Step 2: Understand impact
Missing break can cause unintended multiple prints; adding break after case 1 fixes this.Final Answer:
Missing break after case 1 -> Option AQuick Check:
Each case needs break to stop fall-through [OK]
- Ignoring missing break causes fall-through
- Thinking default is mandatory
- Assuming variable declaration error
Solution
Step 1: Group cases for same output
Cases 1 to 5 print "Weekday", so group them before one printf and break.Step 2: Group weekend cases
Cases 6 and 7 print "Weekend" similarly grouped with break.Step 3: Check default case
switch(day) { case 1: case 2: case 3: case 4: case 5: printf("Weekday\n"); break; case 6: case 7: printf("Weekend\n"); break; default: printf("Invalid\n"); } includes default for invalid days, which is good practice.Final Answer:
Groups weekday cases 1-5 and weekend cases 6-7 with breaks and default -> Option BQuick Check:
Group cases with same output, add break after each group [OK]
- Printing wrong labels for grouped cases
- Missing breaks causing fall-through
- Not handling default case
Ready to prove it?
Pass the test and this pattern moves unknown
