Stop 03 · Control Flow, Methods & ArraysTopics · 4/8

Break & Continue

Part of the Java Roadmap

In short

Sometimes you find what you were looking for halfway through a loop, or you want to skip one bad item without stopping everything. break and continue give you that fine control.

break exits the nearest loop or switch, and continue skips to the next iteration. Labelled break can exit an outer loop from inside a nested one.

The jaadu sorts mangoes from a market crate, tossing a bruised one aside and holding up one perfect golden mango triumphantly.

Think of it like this

Picking mangoes at the market

You check mangoes one by one. A bruised one? Skip it and pick up the next: that is continue. Found the perfect Alphonso you came for? Stop searching and go home: that is break.

Watch it happen

Find the first order above ₹1,000, skipping cancelled ones

450
0
CANCELLED
1
800
2
1250
3
2000
4

1/5Look at order 1: ₹450. Not above 1,000. Keep going.

The big ideas

What you need to know

break exits

Leaves the nearest loop or switch completely. Code after the loop runs next.

continue skips

Jumps to the next iteration. In a for loop, the update (i++) still runs.

Labelled break

outer: for (...) { for (...) { break outer; } } exits both nested loops at once.

Use sparingly

A few breaks make search loops clear. Too many make code hard to follow; a method with return is often cleaner.

See it in code

Walk through the code

java
1public class Search {2    public static void main(String[] args) {3        int[] orders = {450, -1, 800, 1250, 2000}; // -1 = cancelled4 5        int found = -1;6        for (int amount : orders) {7            if (amount < 0) continue;      // skip cancelled8            if (amount > 1000) {9                found = amount;10                break;                     // stop searching11            }12        }13        System.out.println("First big order: " + found);14 15        int[][] seats = {{0, 1, 1}, {1, 1, 0}, {1, 1, 1}};16        search:17        for (int r = 0; r < seats.length; r++) {18            for (int c = 0; c < seats[r].length; c++) {19                if (seats[r][c] == 0) {20                    System.out.println("Free seat at row " + r + ", col " + c);21                    break search;22                }23            }24        }25    }26}

1/4Cancelled orders are skipped. The loop moves to the next amount.

continue skips bad data, break stops at the first match, and a labelled break exits nested loops.

Save yourself the pain

Common mistakes

Oops

Using a plain break inside a nested loop and expecting both loops to stop.

Do this instead

break only exits the innermost loop. Use a labelled break, or move the search into a method and return.

Oops

In a while loop, putting continue before the line that increments the counter.

Do this instead

The increment is skipped and the loop never ends. Update the counter before continue, or use a for loop.

Your mission

Skip and stop

~15 min

Quick check

What does continue do inside a for loop?

Go deeper

  1. 1Stop a search loop with break once you find the target.
  2. 2Skip odd numbers in a loop with continue.
  3. 3Try a labelled break to exit nested loops.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge