Understanding Java Jump Statements: break, continue, and labeled break
This tutorial explains Java's jump statements—break, continue, and the labeled break—showing their syntax, how they affect single and nested loops, and best‑practice considerations through concrete code examples and execution results.
break statement
The break statement can terminate a switch block or exit a loop immediately. When placed inside a loop, execution jumps to the statement following the loop. If the loop is nested, break only exits the innermost loop. break; Example: calculate the area of circles with radii 1‑10 and stop the loop when the area exceeds 100.
final int PAI = 3.14; // π
for (int r = 1; r <= 10; r++) {
double area = PAI * r * r; // area = π·r²
if (area > 100) break;
System.out.println(area);
}Running this code prints areas up to the first value greater than 100, then exits the for loop.
When break appears in a nested loop, only the inner loop stops. The outer loop continues unless it also encounters a break.
public class BreakTest1 {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
System.out.print("line " + i + ": ");
for (int j = 0; j < 5; j++) {
if (j == 2) break; // exit inner loop when j == 2
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("Loop finished.");
}
}Output shows that the inner loop stops at j == 2 while the outer loop proceeds:
line 0: 0 1
line 1: 0 1
line 2: 0 1
line 3: 0 1
Loop finished.Guidelines: avoid excessive break statements because they can reduce readability; prefer loop‑condition checks for normal termination. Use break only for special cases, such as exiting multiple levels with a labeled break.
labeled break
Java allows a break to specify a label, causing the program to exit the labeled block, which may span several nested loops. break labelName; Example demonstrates exiting both inner and outer loops when a condition is met.
public class LabeledBreakDemo {
public static void main(String[] args) {
outer: for (int i = 0; i < 4; i++) {
for (int j = 0; j < 100; j++) {
if (j == 10) break outer; // exit both loops
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("Loop finished.");
}
}Running this program prints numbers 0‑9 of the first inner loop, then terminates both loops immediately.
continue statement
The continue statement skips the remainder of the current loop iteration and proceeds with the next iteration. It is valid only inside loop constructs. continue; Example prints numbers from 1 to 100 that are not divisible by 3.
public class ContinueTest {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) continue; // skip multiples of 3
System.out.print(i + " ");
}
}
}When i is a multiple of 3, the continue skips the print statement, so only non‑multiples appear in the output.
Both break and continue are jump statements, but they differ: break exits the loop entirely, while continue only skips to the next iteration.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
