DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSJAVA FUNDAMENTALS

Switch in Java: Matching a Case Does Not Stop Execution

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 25

A switch starts execution at the case whose label equals the selector

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".

JAVAEvery case in the menu ends with break.
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.

CHECKPOINT 1Not answered

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 matching case does not stop execution by itself

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.

JAVAWithout the break after case 2, execution falls into case 3.
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.

the control-flow path through the menu switch when menuChoice is 2 and case 2 has no breakA flow diagram begins with menuChoice = 2. Case 1 is marked skipped. Case 2 is marked matched and leads to the output "Deposit". Because case 2 has no break, an arrow labeled fall-through goes directly into the case 3 body without a new comparison. Case 3 prints "Withdraw", and its break sends execution out of the switch.menuChoice = 2enter switchcase 1skipped (1 ≠ 2)case 2output: DepositNOBREAKcase 3output: Withdrawcase 3 break→ exit switch1 ≠ 2: skip2 = 2: matchfalls through — no comparisoncontinues
Without break after case 2, execution continues directly into case 3.

The default branch runs only when no case label matches

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.

JAVAChanging menuChoice to 4 reaches default.
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.

CHECKPOINT 2Not answered

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.

Case labels represent fixed choices, not arbitrary conditions

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.

JAVAThese labels describe exact menu values.
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.

Previous · Break and Continue in JavaNext part · Quiz