Switch statement execution in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to run a switch statement changes as we add more cases.
Specifically, how does the number of cases affect the work done when the switch runs?
Analyze the time complexity of the following code snippet.
int GetDayNumber(string day) {
switch(day) {
case "Monday": return 1;
case "Tuesday": return 2;
case "Wednesday": return 3;
case "Thursday": return 4;
case "Friday": return 5;
case "Saturday": return 6;
case "Sunday": return 7;
default: return 0;
}
}
This code returns a number for each day of the week using a switch statement.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Hash computation on the input string and lookup in an optimized table generated by the compiler.
- How many times: Constant time, regardless of the number of cases.
As the number of cases grows, the switch still performs a constant amount of work thanks to compiler optimizations like perfect hashing for strings.
| Input Size (number of cases) | Approx. Operations |
|---|---|
| 7 | ~1 hash + lookup |
| 70 | ~1 hash + lookup |
| 700 | ~1 hash + lookup |
Pattern observation: The number of operations remains constant regardless of the number of cases.
Time Complexity: O(1)
This means the time to find the matching case is constant, independent of the number of cases.
[X] Wrong: "Switch statements check cases one by one, so time grows linearly with the number of cases, O(n)."
[OK] Correct: Modern C# compilers optimize switch statements on strings using hash tables or perfect hashes, enabling constant-time average case lookups.
Understanding switch optimizations demonstrates knowledge of low-level compiler behavior and efficient data structures.
"What if this was implemented as a series of if-else if statements? How would the time complexity change?"
Practice
What is the main purpose of a switch statement in C#?
Solution
Step 1: Understand the role of switch
A switch statement selects one of many code blocks to execute based on a variable's value.Step 2: Compare with other options
Loops repeat code, functions define reusable code, and variable declarations create variables, none of which match switch's purpose.Final Answer:
To execute different code blocks based on the value of a variable -> Option BQuick Check:
Switch chooses code by value = A [OK]
- Confusing switch with loops
- Thinking switch defines functions
- Believing switch declares variables
Which of the following is the correct syntax to start a switch statement in C#?
?
Solution
Step 1: Recall switch syntax
The correct syntax uses parentheses around the variable and braces around cases: switch (variable) { ... }Step 2: Check each option
switch (variable) { case 1: break; } matches correct syntax; others miss parentheses or braces or have wrong order.Final Answer:
switch (variable) { case 1: break; } -> Option AQuick Check:
Parentheses and braces required = C [OK]
- Omitting parentheses around variable
- Missing braces around cases
- Incorrect order of keywords
What will be the output of this C# code?
int day = 3;
switch (day) {
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}Solution
Step 1: Identify the switch variable value
The variable day is 3.Step 2: Match case with variable
Case 3 matches, so it prints "Wednesday" and then breaks.Final Answer:
Wednesday -> Option AQuick Check:
day=3 prints Wednesday [OK]
- Forgetting break causes fall-through
- Choosing default when case matches
- Mixing case numbers and output
Find the error in this switch statement and select the fix:
int number = 2;
switch (number) {
case 1:
Console.WriteLine("One");
case 2:
Console.WriteLine("Two");
default:
Console.WriteLine("Default");
}Solution
Step 1: Identify missing break statements
Each case lacks a break, causing fall-through to next cases.Step 2: Fix by adding breaks
Adding break after each case stops fall-through and executes only matched case.Final Answer:
Add break; after each case block -> Option DQuick Check:
Break stops fall-through = A [OK]
- Ignoring fall-through behavior
- Removing default case unnecessarily
- Misusing if instead of switch
Given this code, what will be printed?
int score = 85;
switch (score / 10) {
case 10:
case 9:
Console.WriteLine("Excellent");
break;
case 8:
Console.WriteLine("Good");
break;
case 7:
case 6:
Console.WriteLine("Pass");
break;
default:
Console.WriteLine("Fail");
break;
}Solution
Step 1: Calculate switch expression value
score is 85, so score / 10 is 8 (integer division).Step 2: Match case for value 8
Case 8 prints "Good" and breaks.Final Answer:
Good -> Option CQuick Check:
85/10=8 prints Good [OK]
- Not using integer division
- Missing combined cases for 9 and 10
- Choosing default incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
