Introduction
A chain of else if that tests the same variable against a list of fixed values gets repetitive fast:
if (mode == 1) { ... }
else if (mode == 2) { ... }
else if (mode == 3) { ... }
The variable is written out every time, and a typo in any one of them is a bug that hides.
switch says it once:
switch (mode) {
case 1: ... break;
case 2: ... break;
case 3: ... break;
}
Same behaviour, less repetition, and the shape of the code makes the intent obvious at a glance: one variable, several fixed possibilities.
The thing that surprises everyone is break. Without it, execution does not stop at the end of a case — it falls through into the next one and keeps going. That is occasionally useful and usually a bug, and it is the reason switch has a reputation.
switch only works on whole numbers and characters, and only against constants. A range like > 30 cannot be a case. When you need that, if is still the right tool.
What you will be able to do
By the end of this lesson you can:
- Write a
switchwith severalcaselabels and adefault. - Say what
breakdoes and what happens without it. - Use deliberate fall-through to group cases.
- Say which types
switchaccepts, and which conditions it cannot express. - Choose between
switchand anifchain for a given problem.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
The shape
int mode = 2;
switch (mode) {
case 1:
Serial.println("Off");
break;
case 2:
Serial.println("Slow");
break;
case 3:
Serial.println("Fast");
break;
default:
Serial.println("Unknown");
break;
}
switch looks at mode once, jumps to the matching case, and runs from there until it meets a break.
default catches anything with no case of its own. It is optional and you should almost always have one.
break is not optional
Leave it out and execution falls through into the next case:
switch (mode) {
case 1:
Serial.println("Off");
// no break!
case 2:
Serial.println("Slow");
break;
}
With mode = 1 this prints both Off and Slow. The match decides where you start, not where you stop.
This is the classic switch bug. If a case behaves oddly, look at the line above it.
Fall-through on purpose
Challenges
Challenge 1
A menu.
Set int choice by hand and print:
| Value | Output |
|---|---|
| 1 | Start |
| 2 | Stop |
| 3 | Reset |
| anything else | Unknown option |
Test 1, 2, 3, 0 and 99.
Then delete the break from case 1 and run it with choice = 1 again. Write down exactly what appears and why.
Log in to ask for the answer.
Challenge 2
Group the cases.
A dice roll produces a category:
- 1 or 2 →
Low - 3 or 4 →
Middle - 5 or 6 →
High
Write it with stacked case labels and three break statements in total, not six.
Roll a real random number every two seconds and print the roll and its category.
Log in to ask for the answer.
Challenge 3
Switch or if?
For each of these, say which tool fits and why. Then write the one that fits.
- A mode variable that is 1, 2 or 3.
- A temperature that is hot above 30, warm above 25, cool otherwise.
- A single letter grade: A, B, C, D or F.
- A score from 0 to 100 that becomes a grade.
Two of them are switch and two are if. The reason is the same in both cases.
Log in to ask for the answer.
Extra challenge
A traffic light state machine.
Keep an int state that cycles 0 → 1 → 2 → 0, changing once a second using millis() rather than delay().
Use a switch on the state to print what the light is doing:
0 Green - go
1 Yellow - slow down
2 Red - stop
Each state should also print how long it will last: green 5 s, yellow 2 s, red 5 s. Move on when that state's own time is up, so the states last different lengths.
Think about it: you have just written a state machine — a variable that remembers where you are, and a switch that decides what to do there. Almost every interactive program is one. What would you have to add so a button could force the light to red immediately?
Log in to ask for the answer.