switch Statements & Expressions
Fall-through and break, then the arrow form: value-producing, exhaustive, compiler-checked.
When one variable is compared against several constant values, switch
reads better than an if ladder:
switch (choice) {
case 1:
System.out.println("Coffee");
break;
case 2:
System.out.println("Tea");
break;
default:
System.out.println("Unknown");
break;
}
switch works on int, long, char, String, enums, and a few more.
Two rules define the classic form:
breakexits the switch. Without it, execution falls through into the next case โ occasionally useful deliberately ("cases 1 and 2 share this code"), famously dangerous by accident. Miss abreakand the compiler stays silent while your menu prints everything below it.defaultis the else โ the case for every value you did not list. Always write one, even when you believe the values are closed.
The modern form: switch expressions (Java 14+). The arrow -> runs
exactly one branch, no fall-through, no break, and the whole switch
produces a value:
String drink = switch (choice) {
case 1 -> "Coffee";
case 2 -> "Tea";
default -> "Unknown";
};
Multiple labels share one arm: case 1, 2 -> "Hot drink";. When an arm
needs several statements, use braces and yield:
String label = switch (code) {
case "A" -> "approved";
default -> {
System.out.println("unknown code: " + code);
yield "rejected";
}
};
A switch expression must be exhaustive โ the compiler forces a default (or all cases covered) because the result must always exist. The compiler checking your decision table for completeness is exactly the kind of ally this course keeps returning to.
Next: designing validations that fail loudly and politely.