Bird
Raised Fist0
Javaprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does a labeled break statement do in Java nested loops?
easy
A. It skips the current iteration of the innermost loop.
B. It pauses the loop until a condition is met.
C. It restarts the innermost loop from the beginning.
D. It exits the loop identified by the label immediately.

Solution

  1. Step 1: Understand labeled break usage

    A labeled break exits the loop that has the specified label, not just the innermost loop.
  2. Step 2: Compare with normal break

    Normal break exits only the innermost loop, but labeled break can exit outer loops directly.
  3. Final Answer:

    It exits the loop identified by the label immediately. -> Option D
  4. Quick Check:

    Labeled break = exit labeled loop [OK]
Hint: Labeled break jumps out of the named loop instantly [OK]
Common Mistakes:
  • Confusing labeled break with continue
  • Thinking it only affects the innermost loop
  • Assuming it pauses instead of exits
2. Which of the following is the correct syntax for a labeled continue statement in Java?
easy
A. continue: labelName;
B. continue labelName;
C. continue (labelName);
D. continue->labelName;

Solution

  1. Step 1: Recall labeled continue syntax

    The correct syntax is the keyword continue followed by the label name and a semicolon.
  2. Step 2: Eliminate incorrect forms

    Java does not use colons, parentheses, or arrows with continue statements.
  3. Final Answer:

    continue labelName; -> Option B
  4. Quick Check:

    Correct labeled continue syntax = continue labelName; [OK]
Hint: Use 'continue' followed by label and semicolon [OK]
Common Mistakes:
  • Adding colon after continue
  • Using parentheses around label
  • Using arrow symbols instead of space
3. What is the output of the following Java code?
outer:
for (int i = 1; i <= 3; i++) {
    inner:
    for (int j = 1; j <= 3; j++) {
        if (j == 2) continue outer;
        System.out.print(i + "-" + j + " ");
    }
}
medium
A. 1-1 2-1 3-1 3-2
B. 1-1 1-2 1-3 2-1 2-2 2-3 3-1 3-2 3-3
C. 1-1 2-1 3-1
D. 1-1 2-1 2-2 3-1 3-2

Solution

  1. Step 1: Analyze loops and continue

    When j == 2, the statement continue outer; skips the rest of the current outer loop iteration and moves to the next i.
  2. Step 2: Trace output

    For i=1, j=1 prints "1-1 ". At j=2, continue outer skips to i=2. Similarly for i=2 and i=3, only j=1 prints.
  3. Final Answer:

    1-1 2-1 3-1 -> Option C
  4. Quick Check:

    Labeled continue skips to next outer loop iteration [OK]
Hint: Labeled continue skips to next iteration of labeled loop [OK]
Common Mistakes:
  • Thinking continue skips only inner loop
  • Expecting all inner loop values to print
  • Ignoring label effect on continue
4. Identify the error in this Java code snippet using labeled break:
outer:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j) break;
    }
}
medium
A. The label 'outer' is not used with break or continue.
B. The break statement should be 'break outer;' to use the label.
C. Labels cannot be used with for loops.
D. The code will cause a syntax error due to missing semicolon.

Solution

  1. Step 1: Check label usage

    The label 'outer' is declared but never referenced by break or continue statements.
  2. Step 2: Understand label necessity

    Labels must be used with break or continue to have effect; otherwise, they are redundant but not syntax errors.
  3. Final Answer:

    The label 'outer' is not used with break or continue. -> Option A
  4. Quick Check:

    Unused label = no effect but no syntax error [OK]
Hint: Use label only if break or continue references it [OK]
Common Mistakes:
  • Assuming unused label causes syntax error
  • Thinking break without label breaks outer loop
  • Confusing label usage rules
5. Given nested loops, how would you use labeled break and continue to print only pairs (i, j) where i < j, skipping all others efficiently? Choose the correct code snippet.
hard
A. outer: for (int i = 1; i <= 3; i++) { inner: for (int j = 1; j <= 3; j++) { if (i >= j) continue inner; System.out.print("(" + i + "," + j + ") "); } }
B. outer: for (int i = 1; i <= 3; i++) { inner: for (int j = 1; j <= 3; j++) { if (i >= j) break outer; System.out.print("(" + i + "," + j + ") "); } }
C. outer: for (int i = 1; i <= 3; i++) { inner: for (int j = 1; j <= 3; j++) { if (i >= j) continue outer; System.out.print("(" + i + "," + j + ") "); } }
D. outer: for (int i = 1; i <= 3; i++) { inner: for (int j = 1; j <= 3; j++) { if (i >= j) break inner; System.out.print("(" + i + "," + j + ") "); } }

Solution

  1. Step 1: Understand the condition and loops

    We want to print pairs where i < j only. If i >= j, skip to next j (inner loop iteration).
  2. Step 2: Analyze each option's control flow

    outer: for (int i = 1; i <= 3; i++) { inner: for (int j = 1; j <= 3; j++) { if (i >= j) continue inner; System.out.print("(" + i + "," + j + ") "); } } uses continue inner; to skip the current iteration and move to next j, efficiently skipping invalid pairs.
  3. Final Answer:

    Uses continue inner; to skip when i >= j. -> Option A
  4. Quick Check:

    Labeled continue to inner loop skips invalid pairs [OK]
Hint: Use 'continue inner;' to skip to next inner loop iteration [OK]
Common Mistakes:
  • Using break outer which stops all loops
  • Using continue outer which skips valid pairs as well
  • Using break inner which stops inner loop prematurely
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard