Modern Switch Statements with Arrow Labels
The traditional switch uses a colon after each case label, and a case label may hold one constant before Java 17.
Try it: while Loops, break and continue
How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.
How it works
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
- If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.
Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.
Loading the simulation…
Testing the Belief About break
Many people who learn switch statements come away believing that every case must end with a break, as if the language demanded it the way it demands a semicolon. The belief is understandable. Most switch statements in older code do end each case with break. But the rule is not part of the syntax itself. This article tests that belief, works out what the syntax really requires, and then looks at the behaviors that make the traditional form tricky.
What do you think happens?
In the program below, the first case has no break. What does the program print when n holds 1?
Reveal answer
one and then two
The case 1 label matches, so its statement runs and prints one. Because that group has no break, control does not leave the switch. It falls through into the case 2 group, which prints two and then reaches its break. The case 3 group is never reached.
one
twoHold your prediction for the rest of the article. The output above is the answer to the question. The next sections explain exactly why the program behaves that way, and why the missing break is the reason for the extra line of output.
Tracing the Program Step by Step
Trace the program with n equal to 1. The switch evaluates n once and gets 1. It compares that value with the constants in the case labels and finds a match at case 1. Execution begins with the statement under case 1, which prints one. There is no break after it, so the computer does not leave the switch. It moves on to the statements under case 2, which prints two. That group ends with break, so execution jumps past the closing brace. The case 3 group and the default group are never executed.
The diagram shows the central idea of this article. Nothing in the jump to the matching label stops execution. Execution simply keeps reading statements in order until it meets a break or the closing brace. Here the case 1 group has no break, so control falls into the case 2 group.
What the Colon Form Requires
A traditional switch statement begins with the keyword switch followed by an expression in parentheses. The expression is evaluated once. Its body is enclosed in braces and contains a series of case labels. Each case label is written as the keyword case, then a constant, then a colon. Under each label sits a group of statements. The colon at the end of the label marks the traditional form. The newer style replaces that colon with an arrow. Both forms serve the same purpose of choosing one group of statements to run.
- switch and the parenthesized expression: evaluated once
- case followed by a constant and a colon: the colon marks the traditional form
- a group of statements under each label: can be as many as needed
- default followed by a colon: the optional fallback group
Why break Is a Decision
The break statement is what tells the computer to leave. A break at the end of a case causes execution to jump past the closing brace of the switch, skipping every remaining case. The syntax does not force you to write one. A program with no break at all will still compile. Whether a break is present or absent is therefore a decision about the behavior you want, and that decision is where most switch bugs begin.
Fall through is the name for the continuation that happens when a case finishes without a break. Control falls from one case into the next, much as water spills over a step into the level below. The source notes that fall through is legal Java and is occasionally useful. Its most natural use is the grouping pattern shown next.
weekday- case 1 has no statements, so it falls into the group below it
- case 7 is the group's real body, and its break ends the switch
- when day is 6, no label matches and the default group runs
Grouping labels on purpose
Given the grouping code above, predict the output when day holds 7, then check your reasoning against the steps.
- 1Evaluate the switch
The expression day is evaluated once and gives 7.
- 2Find the matching label
The case 7 label matches, so execution starts at its body.
- 3Run the body
The body prints weekend.
- 4Check for break
The body ends with break, so control leaves the switch and the default group does not run.
The program prints weekend. The default group does not run because a label matched.
Where the Default Case Fits
The default case is the fallback group of a switch. It is introduced by the keyword default followed by a colon, and it runs when none of the other case labels match the expression. The default case is optional. A switch with no default is perfectly valid. If no label matches in that situation, the switch does nothing and execution continues after the closing brace.
Placement matters for reading, though the source does not require any particular position. In the traditional examples the default group appears last, which is the most common convention. Its statements follow the same fall through rules as any other group. If the default group is last and has no break, the switch simply ends there. If you place the default in the middle, a missing break will cause it to fall into whatever comes next.
Mistakes with break and default
Common mistake Assuming every case must end with break
Why it failsThe break is not required by the syntax. It is the instruction that makes execution jump past the end of the switch.
Do this insteadDecide for each case whether control should leave the switch or continue into the next group, and write break only when you want to leave.
Common mistake Forgetting break after the last statement of a case
What it looks likeA case prints one and then the next case also prints, even though the programmer meant only one output.
Why it failsThe program still runs and produces output, just extra output that nobody intended, so the bug is easy to miss.
Do this insteadEnd every case that should stop with a break, and trace the switch for each value you care about.
Common mistake Expecting the default to run whenever a case matches
Why it failsThe default case runs only when no other case label matches the expression.
Do this insteadUse the default for the no-match situation, and check that each matching case reaches its own break.
Common mistake Writing several constants in one label before Java 17
Why it failsA case label could hold only one constant before Java 17, so a comma-separated list was not allowed in that version.
Do this insteadUse stacked labels, one constant per label, with no statements between them, and recognize that Java 17 allows a comma-separated list.
Common mistake Believing a switch without a default is invalid
Colon Form and Arrow Form Side by Side
The title of this article refers to the arrow style, which the source describes as the newer way to write switch labels. The source says the arrow replaces the colon and serves the same purpose of choosing one group of statements to run. The source material for this article focuses on the traditional colon form, so the table below lists only what the source supports. It does not give arrow-form rules beyond the replacement of the colon.
| Feature | Traditional colon form | Arrow form (as described in source) |
|---|---|---|
| Label ending | A colon after each case label | An arrow replaces the colon |
| Purpose | Chooses one group of statements to run | Same purpose as the colon form |
| Constants per label before Java 17 | Exactly one constant | Not covered by this source |
| Constants per label in Java 17 and later | Comma-separated list allowed | Not covered by this source |
| Role of break | Makes execution jump past the end of the switch | Not covered by this source |
Only the features the source pack describes are compared. Entries marked as not covered should be checked against the arrow-form lesson.
Practice Tracing Switch Flow
Using the first program in this article, predict the output when n holds 2, then when n holds 3, and then when n holds 9. Write down the output for each value before checking the explanation below.
Hints
- For n equal to 2, the case 2 group runs and ends with break.
- For n equal to 3, the case 3 group runs and ends with break.
- For n equal to 9, no case matches and the default group runs. Its body prints other.
Modify the first program so that the case 1 group has a break. Then predict the output for n equal to 1 before running it. Explain in one sentence why the output changed.
Hints
- Count the statements that run before control reaches a break.
- A break at the end of case 1 causes execution to jump past the closing brace.
Key Takeaways
- The traditional switch uses a colon after each case label, evaluates its expression once, and jumps to the label whose constant matches.
- The break statement is not required by the syntax. It makes execution jump past the end of the switch, and omitting it causes fall through into the next case.
- The default case is optional and runs only when no other case label matches the expression. Placing it last is the usual convention, and a missing break in a middle default causes it to fall into the next group.
- Before Java 17 a case label could hold only one constant, and Java 17 allows a comma-separated list of constants in one label.