Control Flow · Lesson 12 of 42
break, continue and Labels
Sometimes a loop should stop early – you found what you were searching for – or skip one pass – this record is invalid. break and continue handle both, and Java's labelled versions solve the "how do I leave two loops at once?" problem.
In this lesson
breakto leave a loop (or switch)continueto skip to the next pass- Labelled
breakandcontinuefor nested loops - Cleaner alternatives: return from a method, stream methods
break: stop now
public class Main {
public static void main(String[] args) {
int[] orders = {250, 480, -1, 300, 120};
for (int amount : orders) {
if (amount < 0) {
System.out.println("Invalid order found – stopping");
break;
}
System.out.println("Processing ₹" + amount);
}
System.out.println("Loop finished");
}
}
Processing ₹250 Processing ₹480 Invalid order found – stopping Loop finished
continue: skip this pass
public class Main {
public static void main(String[] args) {
String[] emails = {"asha@mail.com", "", "ravi@mail.com", "bad-email", "om@mail.com"};
int sent = 0;
for (String e : emails) {
if (e.isBlank() || !e.contains("@")) {
continue; // skip invalid ones
}
System.out.println("Sending to " + e);
sent++;
}
System.out.println(sent + " emails sent");
}
}
Sending to asha@mail.com Sending to ravi@mail.com Sending to om@mail.com 3 emails sent
Inside a loop body
Which keyword?
- breakleave the loop completely – continue after it
- continueabandon this pass – go to the next one (for runs the update step first)
- returnleave the whole method
Common mistake: continue in a while loop skipping the update
In while (i < 10) { if (i % 2 == 0) continue; … i++; } the continue jumps over i++, so i never changes and the loop runs forever. Update the counter before the continue, or use a for loop where the update always runs.
Labelled break: leaving nested loops
A plain break exits only the innermost loop. To exit an outer loop, put a label before it and break with that label:
public class Main {
public static void main(String[] args) {
int[][] seats = {
{1, 1, 1, 1},
{1, 1, 0, 1},
{0, 0, 0, 0},
};
int row = -1, col = -1;
search:
for (int r = 0; r < seats.length; r++) {
for (int c = 0; c < seats[r].length; c++) {
if (seats[r][c] == 0) {
row = r; col = c;
break search; // leaves both loops
}
}
}
System.out.println("First free seat: row " + (row + 1) + ", seat " + (col + 1));
}
}
First free seat: row 2, seat 3
Labelled continue
public class Main {
public static void main(String[] args) {
// Print numbers from 2 to 20 that are prime
outer:
for (int n = 2; n <= 20; n++) {
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) continue outer; // not prime: next n
}
System.out.print(n + " ");
}
}
}
2 3 5 7 11 13 17 19
Often cleaner: return from a method
Labels work, but many teams prefer moving the search into a method and returning as soon as the answer is found. The code reads like a sentence and is easy to test:
public class Main {
static int[] firstFree(int[][] seats) {
for (int r = 0; r < seats.length; r++)
for (int c = 0; c < seats[r].length; c++)
if (seats[r][c] == 0) return new int[]{r, c};
return null; // hall full
}
public static void main(String[] args) {
int[] spot = firstFree(new int[][]{{1, 1}, {1, 0}});
System.out.println(spot == null ? "Full" : "row " + spot[0] + ", seat " + spot[1]);
}
}
row 1, seat 1
break in switch
break also ends a classic switch case. Inside a loop, a break in a classic switch leaves only the switch, not the loop – another reason to prefer arrow-style switches, which need no break at all.
Practice
Try it yourself
Find the first number above 100 that is divisible by both 7 and 9.
Show solution
public class Main { public static void main(String[] args) { int n = 101; while (true) { if (n % 7 == 0 && n % 9 == 0) break; n++; } System.out.println(n); } }126
Sum the numbers 1–20, skipping multiples of 3.
Show solution
public class Main { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 20; i++) { if (i % 3 == 0) continue; sum += i; } System.out.println(sum); } }147
Quick check
Which loop does a plain break leave in nested loops?
Only the innermost loop containing it.
How do you leave an outer loop directly?
Label the outer loop (outer:) and use break outer; – or return from a method.
In a for loop, does the update (i++) run after continue?
Yes. continue jumps to the update step, then to the condition check.
Summary
breakleaves a loop;continueskips to the next pass.- Labels (
outer:) let break/continue target an outer loop. - Moving a search into a method and using
returnis often the cleanest option.
Interview practice: Java output-based and tricky questions.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.