Skip to content

VOL 01 / CH 04 / LESSON 04

4.4 break, continue, return and Labels ​

Prerequisites: while, for, and nested loops Estimated time: 25 minutes You are ready to continue when: You can accurately identify the target of control transfer and the next position.

break Ends the Innermost Loop or switch ​

java
int foundAt = -1;

for (int position = 0; position < 100; position++) {
    if (position == 7) {
        foundAt = position;
        break;
    }
}

System.out.println(foundAt); // 7

Unlabeled break only exits the innermost loop or switch, and continues execution from after the structure. It does not automatically exit the entire method.

continue Skips the Rest of This Iteration ​

java
for (int number = 1; number <= 10; number++) {
    if (number == 5) {
        continue;
    }
    System.out.println(number);
}

Output has no 5.

In for, continue jumps to the update expression, then checks the condition. In while, it jumps to the condition check:

java
int number = 0;
while (number < 10) {
    number++;
    if (number == 5) {
        continue;
    }
    System.out.println(number);
}

If number++ moves after the entire if block, continue skips the update when the value reaches 5. The loop then stays at 5.

return Exiting the Entire Method ​

java
static int findFirst(int[] values, int target) {
    for (int index = 0; index < values.length; index++) {
        if (values[index] == target) {
            return index;
        }
    }
    return -1;
}

return ends the current method call, including any loops inside it. The finally and try-with-resources cleanup still occur according to Java rules; do not interpret return as "immediately skip all cleanup."

Labels Can Escape Multiple Levels ​

java
int foundRow = -1;
int foundColumn = -1;

search:
for (int row = 0; row < 3; row++) {
    for (int column = 0; column < 3; column++) {
        if (row == 1 && column == 2) {
            foundRow = row;
            foundColumn = column;
            break search;
        }
    }
}

break search; exits the entire statement marked search. In Java, labels can be placed before a statement; the target of a label continue must be a loop statement and begins the next iteration of the outer loop.

java
nextRow:
for (int row = 0; row < 3; row++) {
    for (int column = 0; column < 3; column++) {
        if (column == 1) {
            continue nextRow;
        }
        System.out.println(row + "," + column);
    }
}

Labels are a legal tool, but nested searches can often be extracted into a method and used return, or broken down into clearer data operations. Label names should express the target, such as search, rather than using position names like outer1.

C++ Does Not Have a Labeled Break Like Java ​

C++ has goto labels, but does not support break outer; or continue outer;. Exiting nested loops is typically done using function returns, status variables, algorithm abstraction, or in controlled scenarios using goto. Syntax similarity does not equate to equivalent control transfer capability.

Python also does not have a labeled break. It can express multi-level exits through functions return, loops else, helper functions, or exceptions.

Keep the Main Path Clear When Using continue ​

When filtering out invalid items, using continue early can reduce nesting:

java
for (Order order : orders) {
    if (!order.isValid()) {
        continue;
    }
    process(order);
}

However, when there are multiple scattered continue in a round, state updates and resource operations may be skipped. Extracting each round's work into a method or filtering the data first is often easier to verify.

Professional Quick Reference ​

KeywordScopeNext Step
breakInnermost loop or switchAfter the structure
continue in forCurrent iterationUpdate the expression, then check the condition
continue in whileCurrent iterationCheck the condition
returnCurrent method callReturn to the caller
break labelSpecified outer labeled statementAfter the statement
continue labelSpecified outer loopNext iteration

Before Moving On ​

When facing two nested loops, draw the next positions of the unlabeled break, continue, method return, and break search. For the labeled continue, confirm that the label names an enclosing loop. If an exit path requires a lengthy comment to explain, try extracting the method or rewriting the data traversal.

→ Next: Control Flow Experiment

Specification: JLS §14.15–14.17: break, continue, and return.

Built with VitePress | Software Systems Atlas