7.3 Loop Flow Control: break, continue & Labeled Statements
Key Takeaways
An unlabeled break exits only the innermost enclosing loop or switch statement, never a standalone if block
An unlabeled continue skips the rest of the current iteration in the innermost loop, jumping to the update clause in a for loop or directly to the condition in while and do-while loops
Placing a counter increment after a continue inside a while loop easily causes an infinite loop because continue bypasses the remaining body
Labeled break can exit any enclosing loop or labeled statement block, whereas labeled continue must target an enclosing loop label
A labeled jump must target an enclosing statement in the same method, constructor, initializer, or lambda body.
7.3 Loop Flow Control: break, continue & Labeled Statements
When loop termination or iteration skipping depends on mid-body logic, Java provides break, continue, and optional labels to redirect control flow. Java SE 8 exam questions routinely combine nested loops, switch blocks, and labels to test whether you know exactly where execution resumes.
Unlabeled break
An unlabeled break; statement immediately terminates the innermost enclosing loop (while, do-while, for, enhanced for) or switch statement. If the transfer completes, control continues after that construct. A finally block can run before control gets there and can override the transfer.
Two critical rules govern break:
breaknever exits anifstatement on its own. Whenbreak;sits inside anifthat is inside awhile, it exits thewhileloop. Placingbreak;inside anifthat is not nested inside any loop orswitchcauses a compile error (break outside switch or loop).- In nested loops, unlabeled
breakexits only the inner loop. The outer loop continues running its remaining iterations.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break; // Exits ONLY the inner j loop
}
System.out.print(i + "" + j + " ");
}
}
// Output: 11 21 31
Unlabeled continue
An unlabeled continue; skips any remaining statements in the current iteration of the innermost enclosing loop and proceeds to that loop’s next control step. Its condition or traversal may end the loop. Notice the crucial difference in where control lands:
- In a traditional
forloop:continuejumps to theupdateclause (i++), and then evaluates thecondition. - In an enhanced
forloop:continueadvances to the next element. - In a
whileordo-whileloop:continuejumps directly to thecondition, skipping any statements left in the body.
The while + continue Infinite Loop Trap
Because while has no separate update clause, skipping the only progress-making update can produce an infinite loop:
int i = 0;
while (i < 4) {
if (i == 2) {
continue; // Jumps straight back to (i < 4) with i still equal to 2!
}
System.out.print(i + " ");
i++; // Skipped forever once i reaches 2
}
By contrast, placing i++; before the if (...) continue; check—or using a for loop where i++ lives in the update clause—guarantees i advances on every pass. Also remember that continue cannot target a switch statement; if continue appears inside a switch nested within a loop, it skips to the next iteration of that enclosing loop (or fails to compile if no loop encloses the switch).
Labeled Statements (break label; and continue label;)
A label is an identifier followed by a colon (outer:) placed immediately before a statement or block. Labels allow break and continue to target an outer enclosing construct instead of only the innermost one.
outer: for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break outer; // Exits BOTH loops immediately
}
System.out.print(i + "" + j + " ");
}
}
// Output: 00 01 02 10
Replacing break outer; with continue outer; jumps straight to outer's update clause (i++), skipping the rest of the inner loop for the current i and starting the next outer iteration:
outer: for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
continue outer; // Stops inner j loop and jumps to i++
}
System.out.print(i + "" + j + " ");
}
}
// Output: 00 10 20
Valid vs. Illegal Label Targets
Java enforces strict asymmetry between labeled break and labeled continue:
break label;can target ANY enclosing labeled statement or block, including a labeled{ ... }block orifstatement:
checkBlock: {
int score = -5;
if (score < 0) {
break checkBlock; // Valid! Exits the labeled block
}
System.out.println("Valid score");
}
System.out.println("After block"); // Prints: After block
continue label;MUST target an enclosing loop (while,do-while,for, or enhancedfor). Targeting a non-loop block fails compilation (error: not a loop label).- No forward or sibling jumps: Java has no
gotobehavior. Bothbreak label;andcontinue label;must appear inside the labeled statement, and the transfer cannot cross a method, constructor, initializer, or lambda boundary. Jumping out to a sibling label or forward into another block fails compilation (undefined label). - Label attachment trap: A label prefixes a statement. A local variable declaration can appear in a block but cannot be the statement immediately following a label. Attach the label to the loop itself, or to a block enclosing both the declaration and loop:
// outer: int x = 0; // Compile error: declaration cannot follow label
int x = 0;
outer: for (int i = 0; i < 3; i++) {
if (i == x) {
break outer;
}
}
| Statement | Valid Targets | Action in Inner for Inside Outer for |
|---|---|---|
break; | Innermost loop or switch | Exits inner loop; outer loop continues next step |
continue; | Innermost loop only | Jumps to inner loop update (j++) |
break outer; | Any enclosing labeled statement/block | Exits the labeled outer construct completely |
continue outer; | Any enclosing labeled loop | Terminates inner loop; jumps to outer update (i++) |
Caution
Always trace whether continue is labeled or unlabeled and whether the target is a for loop or a while loop. In a nested while loop, continue outer; skips the inner loop and re-evaluates the outer while condition without executing any statements at the bottom of the outer while body.
What does the following Java code print?
outer: for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
continue outer;
}
System.out.print(i + "" + j);
}
}
000102101112202122
00
001020
000210122022
Which statement about labeled control flow in Java SE 8 is true?
Both break label; and continue label; can target a labeled block { ... } that is not a loop
break label; can exit an enclosing labeled block { ... }, whereas continue label; must target an enclosing loop
A label placed right before int x = 0; followed by a for loop attaches to the for loop
break label; can jump forward to a label defined later in the same method
What is the result of executing this code snippet?
int i = 0;
int sum = 0;
while (i < 4) {
i++;
if (i == 2) {
continue;
}
sum += i;
}
System.out.println(sum);
6
10
Infinite loop
8
Sections you finish are checked off in the contents.