Java Tutorial
Java Break and Continue
break leaves a loop early. continue skips the rest of this iteration and starts the next — both keep loops from doing useless work.
continue skips one pass
When continue runs, the rest of the loop body is skipped and the next iteration begins (for afor loop, the step still runs). Use it to filter out cases you do not care about without nesting the whole body in an if.
Example
public class Main {
public static void main(String[] args) {
for (int n = 0; n < 6; n++) {
if (n % 2 == 0) continue;
System.out.println(n); // 1 3 5
}
}
}break leaves the loop
break exits the loop entirely. Use it when you have found what you wanted — searching an array, reading until a sentinel — and further passes would be wasted.
Example
public class Main {
public static void main(String[] args) {
int[] nums = {3, 1, 8, 2};
for (int n : nums) {
if (n > 5) {
System.out.println("first big: " + n); // 8
break;
}
}
}
}Prefer a clear condition on the loop when you can. break and continue are tools for early exit and filtering — not a substitute for a well-written loop bound.
Only the inner loop
In nested loops, break and continue apply to the loop they sit in, not the outer one. To leave both at once, use a labeled break.
Example — labeled break
public class Main {
public static void main(String[] args) {
outer:
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (r == 1 && c == 1) break outer;
System.out.print(r + "," + c + " ");
}
}
System.out.println(); // 0,0 0,1 0,2 1,0
}
}Search with a flag
A common pattern: walk a list, break when you find a match, then check whether you found anything.
Example
public class Main {
public static void main(String[] args) {
String[] names = {"Ada", "Lin", "Grace"};
String target = "Lin";
boolean found = false;
for (String name : names) {
if (name.equals(target)) {
found = true;
break;
}
}
System.out.println(found ? "found" : "missing");
}
}Try It Yourself
Exercise: Print every integer from 1 to 20 that is not divisible by 3. Use continue.
Show solution
for (int n = 1; n <= 20; n++) {
if (n % 3 == 0) continue;
System.out.print(n + " ");
}
System.out.println();When n is a multiple of 3, continue skips the print and moves to the next n.
Key Takeaways
continueskips the rest of the current iteration and starts the next.breakexits the loop entirely — ideal once you have found what you need.- In nested loops, both keywords affect only the innermost loop unless you use a label.
- Clear loop conditions beat heavy use of
break/continue; use them when they simplify the body.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Statistics
Sum odds only
continue skips even samples so the running total only includes odd counts. break would stop the loop entirely; continue just skips one pass.
Example
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int n = 1; n <= 10; n++) {
if (n % 2 == 0) continue;
sum += n;
}
System.out.println(sum);
}
}Physics
Stop at a bad reading
A negative current is a sentinel. break leaves the loop so later averages are not poisoned.
Example
public class Main {
public static void main(String[] args) {
double[] amps = {0.12, 0.11, -1.0, 0.10};
double sum = 0;
int n = 0;
for (double a : amps) {
if (a < 0) break;
sum += a;
n++;
}
System.out.println(sum / n);
}
}