Java Break — Java Technical Interview Questions & Answers

Java Break

Java Break

  • break immediately leaves the nearest loop or switch. In a loop, remaining rounds stop. In switch, later cases do not run.
  • A common pattern: search an array; when you find the value, break. No need to keep looping. continue is different: continue skips one round; break ends the loop.
  • In nested loops, plain break exits only the inner loop. If you expected both to stop, that is a mistake. Trace a loop that breaks at i == 3 and say exactly what prints.
  • Let's take this on the board with one tiny Main class — no extra files. Java Break — output: 1 then 2. When i is 3, break stops the loop — 4 and 5 never print. Start from main, go line by line, and stop at each print. That output is the proof for Break.

Technical Interview Tip: Trace break at i == 3. Say what prints.

Code Examples for Java Break

Example

// Java Break
public class Main {
  public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
      if (i == 3) {
        break;
      }
      System.out.println(i);
    }
  }
}

Java Break — output: 1 then 2. When i is 3, break stops the loop — 4 and 5 never print.

How to Explain Java Break to an Interviewer

What does break do in a loop?

Stops the loop immediately

What does break do in switch?

Leaves the switch Stops fall-through

Does break exit both nested loops?

No Only the inner loop

How is continue different?

continue skips one round break ends the loop