Lesson 26 - Using switch...case Statement
Learn how switch...case selects one path from many options and why break is important.
Progress indicator
Lesson 26 of 28
Learning Objectives
- Understand what switch...case does in Arduino C/C++.
- Write correct switch...case syntax with case labels and default.
- Explain the role of break to avoid fall-through.
- Compare switch...case and if-else for multi-option decisions.
- Trace internal switch execution for matched and unmatched cases.
- Avoid common switch-case mistakes.
Concept Explanation
What is switch...case
switch...case lets your program choose one block of code based on a single value.
It is useful when you have many fixed options like modes, menu selections, or command IDs.
switch...case Syntax
switch (value) {
case option1:
// code
break;
case option2:
// code
break;
default:
// fallback
break;
}How switch...case Works
- Evaluate the switch value once.
- Find matching case label.
- Run code in that case.
- Stop at
break(or fall through if break is missing). - If no case matches, run
defaultblock.
| mode value | Matched block | Result |
|---|---|---|
| 1 | case 1 | LED ON |
| 2 | case 2 | LED OFF |
| 3 | case 3 | Blink once |
| Other | default | Unknown mode message |
case Labels Explained
Each case label represents one specific value (for example 1, 2, 3).
default Case Purpose
default handles unexpected or invalid values safely.
It acts like a fallback so your code still has predictable output.
switch vs if-else (Comparison)
- Use switch for many fixed discrete values.
- Use if-else for ranges or complex conditions.
- switch often becomes cleaner for mode-based logic.
Role of break in switch
break prevents fall-through into the next case.
Missing break can run multiple case blocks unexpectedly.
switch (mode) {
case 1:
Serial.println("Mode 1");
// break missing
case 2:
Serial.println("Mode 2 also runs");
}When to Use switch...case
- Device mode selection
- Menu command handling
- Protocol command routing
Example Code
This example changes LED behavior based on mode value using switch...case.
const int ledPin = 2;
int mode = 2;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
switch (mode) {
case 1:
digitalWrite(ledPin, HIGH);
Serial.println("Mode 1: LED ON");
break;
case 2:
digitalWrite(ledPin, LOW);
Serial.println("Mode 2: LED OFF");
break;
case 3:
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
Serial.println("Mode 3: Blink once");
break;
default:
Serial.println("Unknown mode");
break;
}
delay(1000);
}Example Code Explanation
- Switch reads current
modevalue. case 1turns LED ON and exits with break.case 2turns LED OFF and exits with break.case 3blinks LED once then exits with break.defaultruns when mode is not 1, 2, or 3.
Real-life example
A fan controller can use mode values: 1=Low, 2=Medium, 3=High, default=Safe OFF.
What Happens Inside
- Program evaluates switch value one time per loop cycle.
- Matching case block starts running immediately.
- break exits switch block and prevents extra case execution.
- If no case matches, default block handles fallback path.
- Execution continues after switch block.
Internal flow rule
evaluate value -> jump to case/default -> run block -> break -> continue after switch
Common Mistakes with switch...case
- Forgetting break and causing fall-through bugs.
- Using switch for complex range checks better suited to if-else.
- Missing default case for unknown values.
- Using wrong case labels that never match runtime value.
Best Practices for switch...case
- Use break in each case unless fall-through is intentional.
- Always include default for safety and debugging.
- Keep each case small and focused.
- Use named constants or enums for readable case values.
- Keep shared actions in helper functions to avoid repeated case code.
Practice Task
- Add
case 4for two quick LED blinks. - Set
modeto an unknown value and verify default runs. - Remove one break intentionally and observe fall-through output.
- Replace numeric cases with named constants for readability.
Try it now
Open simulator workspace and practice mode-based control with switch...case.