Codectionary / Developer documentation / Java

break and continue

break and continue give fine-grained control over loop execution in Java. break immediately exits the nearest enclosing loop (or switch statement) entirely. continue skips the rest of the current iteration and jumps straight to the loop's next iteration check. Java also supports labeled break/continue, which can target an outer loop from within a nested one.

Syntax

break;\ncontinue;

Examples

Using break

Exiting a loop as soon as a target condition is found.

int[] numbers = {1, 3, 5, 8, 9, 12};

for (int num : numbers) {
    if (num % 2 == 0) {
        System.out.println("Found first even number: " + num);
        break;
    }
    System.out.println(num + " is odd");
}

Using continue

Skipping specific iterations while letting the loop continue.

for (int i = 1; i <= 10; i++) {
    if (i % 2 != 0) {
        continue;  // skip odd numbers
    }
    System.out.println(i);  // prints only even numbers
}

Labeled break in Nested Loops

Using a label to break out of an outer loop directly from within an inner one.

outerLoop:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
            break outerLoop;  // exits BOTH loops, not just the inner one
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}

Best practices

  • Use break to stop a loop as soon as further iteration is pointless, avoiding wasted work
  • Use labeled break/continue sparingly - they are powerful for nested loops but can make control flow harder to follow if overused
  • Prefer restructuring logic into a separate method with an early return over deeply nested labeled breaks when possible
  • Use continue to skip invalid items cleanly instead of wrapping the rest of the loop body in a large if block

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Variables & Primitive Data Types
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.
System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.
Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.
Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.