Fundamentals 4 min read

Using Labeled break to Exit Multiple Loops in Java

This article explains how Java's break and continue statements work, demonstrates why a simple break cannot exit nested loops, and shows how to use labeled break (and similarly labeled continue) to terminate all loops at once with clear code examples.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Using Labeled break to Exit Multiple Loops in Java

In Java, the break keyword forcibly exits the current loop, while continue skips the remaining statements of the current iteration and proceeds to the next one.

When loops are nested, a plain break only leaves the innermost loop, which can be surprising during interviews or everyday coding.

Example of exiting a single loop:

package com.sample.core.loop;

public class BreakExample {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println("第" + i + "次循环");
            if (i == 3) {
                break;
            }
        }
    }
}

If i reaches 3, the loop terminates and no further iterations are executed, producing output up to the third iteration.

Example of nested loops without a label:

package com.sample.core.loop;

public class BreakExample {
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.println("第" + j + "次内层循环");
                if (j == 1) {
                    break;
                }
            }
            System.out.println("第" + i + "次外层循环");
        }
    }
}

Here, when j equals 1, only the inner loop stops; the outer loop continues, demonstrating that a normal break does not affect outer loops.

Solution with a labeled break to exit all loops:

package com.sample.core.loop;

public class BreakExample {
    public static void main(String[] args) {
        exit_loop:
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.println("第" + j + "次内层循环");
                if (j == 1) {
                    break exit_loop;
                }
            }
            System.out.println("第" + i + "次外层循环");
        }
    }
}

When j reaches 1, the statement break exit_loop; jumps out of both the inner and outer loops, and the program ends after printing the iterations up to that point.

The continue statement can also be used with labels in a similar fashion, though this article does not elaborate on that usage.

Javacontinueloopscontrol flowlabeled breakbreak
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.