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
int foundAt = -1;
for (int position = 0; position < 100; position++) {
if (position == 7) {
foundAt = position;
break;
}
}
System.out.println(foundAt); // 7Unlabeled 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
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:
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
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
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.
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:
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
| Keyword | Scope | Next Step |
|---|---|---|
break | Innermost loop or switch | After the structure |
continue in for | Current iteration | Update the expression, then check the condition |
continue in while | Current iteration | Check the condition |
return | Current method call | Return to the caller |
break label | Specified outer labeled statement | After the statement |
continue label | Specified outer loop | Next 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.