0
0
Javaprogramming~10 mins

Labeled break and continue in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Labeled break and continue
Start Outer Loop
Start Inner Loop
Check Condition
Action
Labeled break or continue
Exit or Continue Outer Loop
Outer Loop End
End
The outer loop starts, then the inner loop runs. Depending on conditions, a labeled break or continue can jump out or skip iterations of the outer loop.
Execution Sample
Java
outer: for (int i = 1; i <= 3; i++) {
  for (int j = 1; j <= 3; j++) {
    if (i == 2 && j == 2) break outer;
    System.out.println(i + "," + j);
  }
}
Print pairs of i and j, but break out of both loops when i=2 and j=2.
Execution Table
StepijCondition (i==2 && j==2)ActionOutput
111falsePrint 1,11,1
212falsePrint 1,21,2
313falsePrint 1,31,3
421falsePrint 2,12,1
522trueBreak outer loop
💡 At step 5, condition is true, so break labeled 'outer' exits both loops.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
i1111222
j11231
Key Moments - 2 Insights
Why does the break labeled 'outer' stop both loops instead of just the inner one?
Because the break uses the label 'outer', it exits the loop labeled 'outer', which is the outer loop, not just the inner loop. See step 5 in execution_table where break outer stops all looping.
What happens if we use 'continue outer;' instead of 'break outer;'?
The continue labeled 'outer' skips the current iteration of the outer loop and starts the next iteration. It does not exit the loops completely. This differs from break which exits.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of j at step 3?
A3
B2
C1
D4
💡 Hint
Check the 'j' column in the execution_table row for step 3.
At which step does the labeled break cause the loops to exit?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look at the 'Action' column where 'Break outer loop' happens.
If the condition was never true, how many times would the inner loop print output for i=1?
A2
B3
C1
D0
💡 Hint
Look at steps 1 to 3 where i=1 and j goes from 1 to 3 printing output.
Concept Snapshot
Labeled break and continue in Java:
- Use 'label:' before a loop to name it.
- 'break label;' exits the named loop immediately.
- 'continue label;' skips to next iteration of named loop.
- Helps control nested loops clearly.
- Syntax example:
  outer: for(...) { for(...) { if(cond) break outer; } }
Full Transcript
This example shows how labeled break works in Java nested loops. The outer loop is labeled 'outer'. The inner loop runs inside it. When the condition i==2 and j==2 is true, the break labeled 'outer' stops both loops immediately. Before that, pairs of i and j are printed. The variable tracker shows how i and j change each step. Key moments explain why the labeled break exits both loops and how continue labeled would behave differently. The visual quiz tests understanding of variable values and loop exit points. The snapshot summarizes how to use labeled break and continue to control nested loops clearly.