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.

Last updated: November 2026

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:

  1. break never exits an if statement on its own. When break; sits inside an if that is inside a while, it exits the while loop. Placing break; inside an if that is not nested inside any loop or switch causes a compile error (break outside switch or loop).
  2. In nested loops, unlabeled break exits 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 for loop: continue jumps to the update clause (i++), and then evaluates the condition.
  • In an enhanced for loop: continue advances to the next element.
  • In a while or do-while loop: continue jumps directly to the condition, 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 or if statement:
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 enhanced for). Targeting a non-loop block fails compilation (error: not a loop label).
  • No forward or sibling jumps: Java has no goto behavior. Both break label; and continue 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;
    }
}
StatementValid TargetsAction in Inner for Inside Outer for
break;Innermost loop or switchExits inner loop; outer loop continues next step
continue;Innermost loop onlyJumps to inner loop update (j++)
break outer;Any enclosing labeled statement/blockExits the labeled outer construct completely
continue outer;Any enclosing labeled loopTerminates 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.

Loading diagram...
Control Flow of break, continue, and Labeled Variants in Nested for Loops
Test Your Knowledge

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);
    }
}
A

000102101112202122

B

00

C

001020

D

000210122022

Test Your Knowledge

Which statement about labeled control flow in Java SE 8 is true?

A

Both break label; and continue label; can target a labeled block { ... } that is not a loop

B

break label; can exit an enclosing labeled block { ... }, whereas continue label; must target an enclosing loop

C

A label placed right before int x = 0; followed by a for loop attaches to the for loop

D

break label; can jump forward to a label defined later in the same method

Test Your Knowledge

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);
A

6

B

10

C

Infinite loop

D

8

Sections you finish are checked off in the contents.