Sub topic 6
Read Colon-Style Cases and Control Fall-Through with break
Find where the switch starts
A switch statement uses one value to pick where to start running code. That value is called the selector. In a colon-style switch, a case label such as case 2: marks a place to start. The colon is the punctuation mark at the end of the label.
Here is the key idea: a case label marks where Java can start, but it does not tell Java where to stop. Once Java finds the matching label, it runs the lines below it in order. If it reaches another label, it can keep going into that case without checking for a match again. This is called fall-through.
A break statement tells Java to leave the switch. In the examples below, Java then runs the first line after the switch. The word break does not end the whole program.
Run a menu example
Save this program as MenuDemo.java. Compile it with javac MenuDemo.java, then run it with java MenuDemo.
public class MenuDemo {
public static void main(String[] args) {
int menuChoice = 2;
switch (menuChoice) {
case 1:
System.out.println("Start");
break;
case 2:
System.out.println("Settings");
break;
default:
System.out.println("Unknown choice");
}
System.out.println("Menu closed");
}
}Before you run it, trace the path:
- The value of menuChoice is 2, so Java starts at case 2. It does not run the lines under case 1.
- Java prints “Settings” and reaches break. It leaves the switch without running the lines under default.
- Java runs the next line after the switch and prints “Menu closed.”
Try changing menuChoice to 1 and then to 9. Save and compile the file again after each change.
| menuChoice | Where Java starts in the switch | Output, in order |
|---|---|---|
| 1 | case 1 | Start, then Menu closed |
| 2 | case 2 | Settings, then Menu closed |
| 9 | default | Unknown choice, then Menu closed |
The default label is the starting point when no case matches. In this program, its print line is the last line inside the switch. It does not need a break because there is no code inside the switch left to run.
Remove one break and trace the result
Set menuChoice back to 2. Delete only the break right after the line that prints “Settings.” Leave the break under case 1 in place. Compile and run the program again.
This time, the program prints “Settings,” then “Unknown choice,” then “Menu closed.” The starting point did not change: Java still matched case 2. The difference is that there was no break after “Settings.” Java kept going and ran the line under default.
Put the break back after “Settings.” Now try a different prediction: if you removed only the break after “Start” and set menuChoice to 1, where would Java stop? It would print both “Start” and “Settings,” then stop at the break under case 2. A break stops the path when Java reaches it, even if the path started at a different label.
Let two labels share one action
Sometimes you want two choices to do the same thing. For example, you might treat both 0 and 9 as a request to exit a menu. Put the two labels next to each other, with no code between them. Then put one action and one break below both labels.
Save this program as ExitChoice.java. Compile it with javac ExitChoice.java and run it with java ExitChoice.
public class ExitChoice {
public static void main(String[] args) {
int choice = 0;
switch (choice) {
case 0:
case 9:
System.out.println("Exit selected");
break;
default:
System.out.println("Not an exit choice");
}
}
}For 0, Java starts at case 0 and continues straight to the shared print line. For 9, Java starts at case 9 and runs that same line. Either way, “Exit selected” prints just once. If you put a break between the labels, a choice of 0 would leave the switch before printing anything.
Trace the path before you run it
When you read a colon-style switch, ask two questions: Where does Java start? Where does it leave? Find the matching label, or start at default if no case matches. Then read down, passing any labels you reach. In the examples here, keep going until you hit a break or the closing brace of the switch.
If a case should do only its own work, put a break after that work. If two choices should do the same work, place their labels together and put the break after the shared code.
Exercise
In MenuDemo, set menuChoice to 1 and remove the break after both “Start” and “Settings.” What prints, in order?
Show answer
Start, Settings, Unknown choice, and Menu closed. Java starts at case 1 and keeps going until the switch ends.
Exercise
In ExitChoice, where should you add case 8: so that 8 also prints “Exit selected”?
Show answer
Put it with case 0: and case 9:, before the shared print line. Keep the break after that line.