Controlling Flow with Branching
Java provides
break,continue, andreturnto alter the normal top-to-bottom execution of loops and methods. Labeled versions extend these to nested loop structures.
break
break terminates the innermost enclosing loop or switch statement immediately:
javafor (int i = 0; i < 10; i++) {
if (i == 5) {
break; // exits the for loop
}
System.out.println(i);
}
// Output: 0 1 2 3 4Execution continues at the first statement after the terminated loop.
continue
continue skips the remainder of the current iteration and moves to the next one:
javafor (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // skip even numbers
}
System.out.println(i);
}
// Output: 1 3 5 7 9In a while or do-while, continue jumps directly to the condition check. In a for loop, it jumps to the update expression first, then checks the condition.
return
return exits the current method entirely, optionally passing a value back to the caller:
javastatic int firstNegative(int[] values) {
for (int v : values) {
if (v < 0) {
return v; // exits the method immediately
}
}
return -1; // no negative found
}return terminates not just the loop but the entire enclosing method, which makes it the strongest of the three branching statements.
Comparing break, continue, and return
| Statement | Scope terminated | Execution resumes at |
|---|---|---|
break |
Innermost loop or switch |
Statement after the loop/switch |
continue |
Current iteration only | Next iteration (update + condition) |
return |
Entire method | Caller of the method |
Labeled Statements
A label is an identifier followed by a colon placed before a statement. Labels are most useful on nested loops:
javaouter:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println(i + "," + j);
}
}By itself, a label has no effect. Its purpose is to be the target of a labeled break or continue.
break with a Label
break <label> exits the labeled statement, not just the innermost one:
javaouter:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
break outer; // exits the OUTER for loop
}
System.out.println(i + "," + j);
}
}
// Output: 0,0Without the label, break would only exit the inner loop and the outer loop would continue.
continue with a Label
continue <label> skips to the next iteration of the labeled loop, bypassing any remaining iterations of inner loops:
javaouter:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
continue outer; // skip rest of inner loop, advance outer i
}
System.out.println(i + "," + j);
}
}
// Output: 0,0 1,0 2,0Label Placement Rules
Labels can be placed on any statement, but the OCP exam focuses on loops:
javasearch:
while (condition) {
// ...
}A label must appear immediately before the statement it names. A break or continue referencing a label must be lexically inside that labeled statement — labels are not jump targets that can be reached from anywhere (Java has no goto).
Unreachable Code
Statements immediately following an unconditional break, continue, or return are unreachable and cause a compile-time error:
javafor (int i = 0; i < 5; i++) {
break;
System.out.println(i); // compile error: unreachable statement
}javastatic void example() {
return;
System.out.println("Never"); // compile error: unreachable statement
}Practical Example: Search in 2D Array
javaint[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int target = 5;
boolean found = false;
search:
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == target) {
found = true;
break search; // stop as soon as target is found
}
}
}
System.out.println("Found: " + found); // Found: trueWithout the labeled break, the inner break would only exit the inner loop, and the outer loop would continue unnecessarily.
Key Rules to Remember
breakandcontinuewithout a label always affect the innermost enclosing loop orswitchcontinuegoes to the update expression in aforloop, then re-checks the condition- A labeled
breakexits the labeled statement entirely; a labeledcontinueadvances to the next iteration of the labeled loop - Labels must be placed immediately before the statement they name
- Code after an unconditional
break,continue, orreturndoes not compile - Java has no
goto— labels are only valid targets forbreakandcontinue