Java Break & Continue: Jump Statements & Labeled Nested Loops

Jump statements transfer program execution control unconditionally to another part of the program. Java provides two primary loop jump statements: break and continue, both of which support unlabeled and labeled variants.

1. The Break Statement (Loop Exit)

The break statement terminates the current loop immediately. Execution resumes at the first statement following the loop closing brace:

for (int i = 1; i <= 10; i++) {
    if (i == 6) {
        System.out.println("Target 6 found! Breaking out of loop.");
        break; // Loop terminates immediately when i is 6
    }
    System.out.print(i + " ");
}
// Output: 1 2 3 4 5 Target 6 found! Breaking out of loop.

2. The Continue Statement (Skip Current Cycle)

The continue statement skips the remainder of the current loop iteration and immediately jumps to the next cycle (evaluating the step update in a for loop, or the boolean condition in a while loop):

for (int i = 1; i <= 6; i++) {
    if (i % 2 == 0) {
        continue; // Skips even numbers
    }
    System.out.print(i + " ");
}
// Output: 1 3 5 (Even numbers 2, 4, 6 skipped!)

3. Labeled Break & Labeled Continue in Nested Loops

In deeply nested loops, standard break and continue only affect the innermost enclosing loop. To break or continue an outer loop from deep within an inner loop, use Labels:

public class LabeledLoopDemo {
    public static void main(String[] args) {
        // Outer loop labeled as 'outerLoop'
        outerLoop:
        for (int row = 1; row <= 3; row++) {
            for (int col = 1; col <= 3; col++) {
                if (row == 2 && col == 2) {
                    System.out.println("Breaking directly out of outerLoop at [2,2]!");
                    break outerLoop; // Exits BOTH inner and outer loops!
                }
                System.out.println("Row: " + row + ", Col: " + col);
            }
        }
        System.out.println("Execution resumed after outerLoop.");
    }
}

Output:

Row: 1, Col: 1
Row: 1, Col: 2
Row: 1, Col: 3
Row: 2, Col: 1
Breaking directly out of outerLoop at [2,2]!
Execution resumed after outerLoop.

University Exam Corner (BCA / B.Sc CS)

Exam Question: "Is 'goto' a keyword in Java? Why does Java not allow using it?"

Answer: goto is a reserved keyword in Java (meaning you cannot name a variable or class goto), but it has no function and cannot be used. James Gosling intentionally excluded goto to prevent programmers from writing unstructured "spaghetti code" that causes unpredictable jumps, memory corruptions, and debugging nightmares.

Next Step

Congratulations on completing Control Flow & Loops! Let us now step into the crown jewel of Java: Object-Oriented Programming (OOP). Proceed to: OOP 4 Pillars in Java →

Frequently Asked Questions (FAQ)

Q1. What is the difference between break and continue in Java?
The 'break' statement immediately terminates the innermost enclosing loop or switch statement, transferring execution to the statement following the loop. The 'continue' statement skips the remaining code in the current iteration and jumps directly to the next loop cycle.
Q2. What are Labeled Break and Labeled Continue statements?
In nested loops, a standard break/continue only affects the innermost loop. By placing an identifier label (e.g. 'outer:') before the outer loop header, 'break outer;' can immediately exit out of all nested loops simultaneously.
Q3. Does Java support the 'goto' statement?
No! 'goto' is a reserved keyword in Java, but it has no implementation and cannot be used. Labeled break and labeled continue provide safe, structured alternatives to goto without spaghetti code risks.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java For Loop →

Iterating through loops.

Java Switch →

Using break in switch cases.

Java While Loop →

Breaking out of while loops.

OOP Concepts Overview →

Entering Section 3: OOP in Java.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!