C Notes
Learn the switch statement in C with syntax, flowchart, fall-through behavior, break, default case, nested switch, and comparison with if-else. Includes interview questions.
The switch statement is a multi-way branching mechanism that lets you select one of many code blocks to execute based on the value of an expression. Think of it as a menu selection — depending on what you choose (1, 2, 3, etc.), a specific action is performed.
While the else-if ladder can handle the same scenarios, the switch statement is often cleaner and more efficient when you're comparing a single variable against several constant values. It makes your code more readable and the compiler can optimize it better using jump tables.
Syntax of Switch Statement
| Component | Description |
|---|---|
expression | An integer or character expression (evaluated once) |
case constant | A compile-time constant value to compare against |
break | Exits the switch block after a case is matched |
default | Optional; executes when no case matches |
Flowchart of Switch Statement
Basic Example
Wednesday
Fall-Through Behavior
One of the most important (and often confusing) aspects of switch in C is fall-through. If you forget to put a break statement, execution continues into the next case — regardless of whether that case matches!
Case 2 Case 3 Case 4
Notice how cases 3 and 4 also executed even though x is 2. This is fall-through — execution falls from one case to the next until a break is encountered.
Intentional Fall-Through
Sometimes fall-through is useful — for example, when multiple cases should execute the same code:
30 days
Practical Examples
Example 1: Simple Calculator
Enter operator (+, -, *, /): * Enter two numbers: 6 7 6.00 * 7.00 = 42.00
Example 2: Menu-Driven Program
=== Temperature Converter === 1. Celsius to Fahrenheit 2. Fahrenheit to Celsius 3. Exit Enter your choice: 1 Enter temperature in Celsius: 100 100.00°C = 212.00°F
Rules and Restrictions
- Expression must be integer type —
int,char, orenum(notfloatorstring) - Case labels must be compile-time constants — no variables allowed
- No duplicate case values — each case must have a unique constant
- Case labels can be in any order — including default
- Default is optional — but recommended
Switch vs if-else Comparison
| Feature | Switch | if-else |
|---|---|---|
| Type of comparison | Equality only | Any relational/logical |
| Expression type | Integer/char only | Any type |
| Range checking | Not directly | Yes (e.g., x > 10) |
| Performance | Often faster (jump table) | Sequential evaluation |
| Readability | Better for many discrete values | Better for ranges/complex conditions |
| Fall-through | Yes (can be useful or dangerous) | No |
Nested Switch Statements
You can place one switch inside another:
Engineering Department Senior Engineer
Best Practices
- Always include
breakunless fall-through is intentional - Comment intentional fall-through —
/* falls through */ - Always include a
defaultcase for unexpected values - Use switch for 3+ discrete values — use if-else for fewer
- Order cases logically — most common cases first or numerically
Interview Questions
Q1: Can we use float values in a switch statement?
No. The switch expression and case labels must be of integer type (int, char, enum, or compatible types). Floating-point numbers cannot be used because exact equality comparison of floats is unreliable.
Q2: What happens if we omit the break statement in a switch case?
Without break, execution "falls through" to the next case regardless of whether that case matches. All subsequent statements execute until a break is encountered or the switch block ends.
Q3: Can the default case appear anywhere in the switch (not just at the end)?
Yes. The default case can appear at any position in the switch. However, placing it at the end is the convention for readability. If placed elsewhere and without a break, fall-through rules still apply.
Q4: Why is switch sometimes faster than if-else?
The compiler can optimize a switch statement into a jump table — an array of addresses indexed by the case value. This provides O(1) constant-time branching regardless of the number of cases, while an else-if ladder requires O(n) sequential comparisons.
Q5: Can two case labels have the same value?
No. Duplicate case values in the same switch are a compilation error. Each case must have a unique constant integer value within its switch block.
Summary
The switch statement is a powerful branching mechanism for selecting among multiple discrete values. It provides cleaner syntax than long else-if chains and potentially better performance through compiler optimizations. Remember the key rules: integer expressions only, constant case labels, use break to prevent fall-through, and always include a default case. When dealing with ranges or non-equality comparisons, stick with if-else instead.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Switch Statement in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, control, statements, switch, statement, switch statement in c programming
Related C Programming Topics