Java Notes
A switch statement chooses one action from fixed values, such as a menu number, a day, or a known status.
Use switch when you have one value and a fixed list of possible choices. For example, a menu can use 1 for Register, 2 for Profile, and 3 for Exit. Java compares the chosen number with each case and runs the matching action.
switch is not for every condition. It is best when you compare one value with exact choices. Use if-else for ranges such as “score is at least 40” or combinations such as “age is 18 and has a ticket.”
Traditional switch: match one menu choice
Imagine a learning app menu. The user chooses a number. The program should perform one matching action.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Java first reads choice, which is 2. It looks for case 2, prints View profile, then meets break and leaves the switch.
Change choice to 1, 3, and 9, then run the program each time. 9 matches none of the listed cases, so Java uses default. default is the fallback action for an unknown or invalid value.
Why break matters
In the traditional form, break means “this case is finished; leave the switch now.” Without it, Java continues into the next case even when that next case does not match the choice. This is called fall-through.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Run this code. Even though choice is 1, Java prints both messages because case 1 has no break. For ordinary menus, add break after every case so only one action runs.
Fall-through can be used deliberately in advanced code, but as a beginner, treat a missing break as a likely bug unless the code clearly explains why it is intentional.
Modern switch: choose a value safely
Modern Java also supports arrow cases. They are useful when the switch should calculate one result and save it in a variable.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
This time day is 6, so Java selects "Weekend" and stores it in type. Arrow cases do not fall through, so no break is needed. You can group matching values with commas, which makes the weekday/weekend rule easy to read.
Use arrow cases when your project uses a current Java version and the switch produces a clear answer. The traditional form is still common in existing code, so understand both.
Switch with text
You can switch on a String when the text is one of a fixed set of commands or roles.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The variable role contains text, and each case contains one possible text value. Keep the cases exact: "student" and "Student" are different because Java treats capital and small letters differently.
Do not switch on missing text. If role might be null, check it before the switch; otherwise Java cannot decide which case to use.
Switch or if-else?
| Use switch when... | Use if-else when... | ||
|---|---|---|---|
One value has fixed choices, such as 1, 2, 3 or "student", "teacher" | You need a range such as score >= 40 | ||
| Each choice has one clear action | You combine conditions using && or ` | ` | |
| You need a menu, day, status, role, or command | You need a complex decision |
For example, use switch for a month number or menu choice. Use if-else for grading marks because marks are ranges, not a few unrelated exact values.
Common mistakes
- Forgetting
breakin traditional cases can run extra code. - Forgetting
defaultmeans an unexpected value may do nothing. - Writing
case 1.5for anintswitch is not allowed; the case type must match the switched value. - Using switch for a range makes code harder to read; use
if-elseinstead.
Try it yourself
Create a class named MonthQuarter. Store a month number from 1 to 12. Use a modern switch expression to print Q1, Q2, Q3, or Q4. Print Invalid month for any other number.
Hint
Months 1, 2, and 3 belong to Q1. Group those values with commas: case 1, 2, 3 -> "Q1";.
One working answer
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Run it with 3, 4, 12, and 13. Those tests show whether the grouped cases and default case work correctly.
Continue the lesson path