Module 3 · Control Flow and Arrays — topic 2 of 4
switch Statements and Expressions
The classic switch statement with break and fall-through, the modern arrow form, switch expressions that return values with yield (Java 14), and switching on strings and enums.
Learning objectives
- Write classic switch statements and explain fall-through
- Use arrow-form cases and switch expressions
- Switch on int, char, String and enum values
Definition
A switch selects one of several branches by comparing a value against constant case labels. A switch expression (Java 14+) produces a value and must cover every possible input.
Introduction
When a decision depends on which of several fixed values a variable has — a menu choice, a day of the week, a command word — a switch is often clearer than a long else-if chain. Modern Java has improved switch considerably: the arrow form has no fall-through, and a switch can now be an expression that produces a value.
Why it matters
Menu-driven programs, command parsers and state machines are naturally written with switch.
Forgetting break in a classic switch is a famous source of bugs; the modern form removes it entirely.
Detailed explanation
Classic statement. switch (x) { case 1: ...; break; case 2: ...; break; default: ...; }. Execution jumps to the matching label and continues until a break — if you omit it, control *falls through* into the next case.
Allowed types. int and smaller integers, char, their wrapper classes, String (compared with equals) and enums. Case labels must be compile-time constants and unique.
Arrow form (Java 14). case 1 -> ...; runs only that case — no fall-through and no break needed. Several labels can share a case: case "sat", "sun" -> .... A case body can be a single expression, a block in braces, or a throw.
switch expressions (Java 14). A switch can produce a value: String type = switch (day) { case 6, 7 -> "weekend"; default -> "weekday"; };. Inside a block, yield value; returns the value. A switch expression must be *exhaustive*: every possible input must be handled, usually with default. For enums, listing every constant is enough.
Pattern matching for switch (Java 21) lets cases test types, for example case Integer i -> ...; you will see it again with sealed types later in your Java journey.
Syntax
- Arrow and colon forms can't be mixed in one switch.
- A switch expression ends with a semicolon because it is part of a statement.
- Switching on a null String or enum throws NullPointerException (unless you use
case nullin Java 21).
Code examples
Classic switch and fall-through
edit rights view rights
Deliberate fall-through: level 2 gets its own rights and everything below. Comment such code clearly.
Arrow cases on strings
start: running STOP: stopped pause: paused jump: unknown command
More examples
switch expressions with yield
29 28 30 31 6 * 7 = 42 bad month 13
Step-by-step explanation
return switch (month) { ... };returns the value produced by the switch expression.case 4, 6, 9, 11 -> 30;groups several labels in one case.- The February case needs several statements, so it uses a block and
yieldto produce its value. default -> throw ...makes the switch exhaustive and rejects invalid months.- Switching on a
charoperator is a compact way to write a small calculator.
Expected output
Running “switch expressions with yield” prints exactly:
Practical applications
- Menu-driven console programs
- Command interpreters and protocol handlers
- Mapping enum values to labels, prices or behaviour
- State machines in games and parsers
Best practices
- Prefer the arrow form in new code.
- Use switch expressions to compute a value instead of assigning a variable in every case.
- Always handle unexpected values with default — often by throwing an exception.
Common mistakes
Missing break in a classic switch
Avoid
Prefer
Without break, case 1 falls through and also prints "two". The arrow form never falls through.
Non-exhaustive switch expression
Avoid
Prefer
A switch expression must produce a value for every input, so the compiler requires a default for int.
Important notes
- Switch expressions became standard in Java 14; pattern matching for switch became standard in Java 21.
- String cases are case-sensitive; normalise with toLowerCase() first, as in the example.
Key takeaways
- Classic switch falls through without break; arrow cases never do.
- switch expressions return a value and must be exhaustive.
- Use yield to return a value from a block inside a switch expression.
Try it yourself
- 1Write a switch expression that converts a day number (1–7) into its name.
- 2Build a menu loop with options 1–4 and 0 to exit using arrow cases.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
6 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerIn a switch expression, what does `yield` do?