PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
A switch compares its selector with each fixed case label until it finds an equal value. The matching case becomes the entry point for execution. With menuChoice set to 2, Java skips case 1, enters case 2, and prints "Deposit".
int menuChoice = 2;
switch (menuChoice) {
case 1:
System.out.println("View balance");
break;
case 2:
System.out.println("Deposit");
break;
case 3:
System.out.println("Withdraw");
break;
default:
System.out.println("Invalid choice");
}The break after case 2 sends execution out of the switch immediately after "Deposit" is printed. Java does not continue testing case 3 or default as if they were separate if conditions. The selector has already chosen where execution starts, and break determines where it stops.
When menuChoice is 2 and every case has a break, which single line is printed?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A case label chooses the entry point, but it is not an automatic exit point. If you remove only the break after case 2, execution prints "Deposit" and then continues directly into the body of case 3. Java does not compare menuChoice with 3 again, so case 3 prints "Withdraw" even though the selector is still 2.
int menuChoice = 2;
switch (menuChoice) {
case 1:
System.out.println("View balance");
break;
case 2:
System.out.println("Deposit");
// no break here
case 3:
System.out.println("Withdraw");
break;
default:
System.out.println("Invalid choice");
}The break after case 3 finally exits the switch, so default is not reached in this trace. The resulting output is two lines: first "Deposit", then "Withdraw". This behavior is called fall-through. It can be deliberate, but leaving out break by accident makes one menu choice perform another menu action.
Restore the break after case 2, then change only menuChoice from 2 to 4. The value 4 does not match case 1, case 2, or case 3, so Java enters default and prints "Invalid choice". Because no earlier case starts execution, there is no case body to fall through from.
int menuChoice = 4;
switch (menuChoice) {
case 1:
System.out.println("View balance");
break;
case 2:
System.out.println("Deposit");
break;
case 3:
System.out.println("Withdraw");
break;
default:
System.out.println("Invalid choice");
}With menuChoice equal to 2, case 2 matches and its break exits before default, so "Invalid choice" is not printed. With menuChoice equal to 4, no case label matches and default runs. Default is not a second action after every case, and it is not reached when a matching case has already exited the switch.
The break after case 2 is missing. Type the exact line that makes menuChoice = 2 print only "Deposit".
case 2:
System.out.println("Deposit");
case 3:Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The labels in this menu, case 1, case 2, and case 3, represent distinct constant values. A switch is a clear fit when one selector must choose among exact choices. A label such as case menuChoice > 1 cannot replace one of them: menuChoice > 1 is a logical condition that produces true or false, not a fixed integer choice matching the int selector.
switch (menuChoice) {
case 1:
System.out.println("View balance");
break;
case 2:
System.out.println("Deposit");
break;
case 3:
System.out.println("Withdraw");
break;
default:
System.out.println("Invalid choice");
}For this menu, case labels answer exact questions: is menuChoice 1, 2, or 3? A range such as menuChoice greater than 1 or a compound test involving several comparisons does not describe one fixed case label. Those decisions need a conditional form that evaluates the logical condition directly.