The switch statement
◈ 2 cardsBranching on one value — and the fall-through trap.
Choosing among many values
A classic switch compares one value against several case labels:
switch (day) {
case 1:
name = "Mon";
break;
case 2:
name = "Tue";
break;
default:
name = "?";
}
The danger is fall-through: without break, execution continues into the next case. Forgetting a break is a real bug source — and occasionally a deliberate trick to share code between cases.
A classic switch works on int/short/byte/char, their wrappers, String, and enum types. The next lesson shows the modern switch expression, which fixes fall-through entirely.