0Pricing
Java Academy · Lesson

break and continue

Skip and exit iterations.

break and continue is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Control a Loop?

Loops repeat code, but sometimes you need to stop early or skip an item.

Java gives you two simple tools for this: break ends the loop completely, and continue skips to the next round.

In this lesson you will learn exactly when each one fires and how they change a loop's flow.

The break Statement

The break keyword stops the nearest loop immediately.

Execution jumps to the first line after the loop. Any remaining iterations are abandoned.

Below, the loop stops as soon as i reaches 3.

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                break;
            }
            System.out.println("i = " + i);
        }
        System.out.println("Done");
    }
}

What break Printed

The program printed i = 1 and i = 2, then Done.

When i became 3 the break fired, so 3, 4, and 5 never printed.

break is perfect when you have found what you need and want to stop searching.

The continue Statement

The continue keyword skips the rest of the current iteration.

The loop does not stop. It jumps back to the loop header and tries the next value.

Here we skip the number 3 but keep going.

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                continue;
            }
            System.out.println("i = " + i);
        }
    }
}

What continue Printed

The output was 1, 2, 4, 5. The value 3 was skipped, but the loop kept running.

That is the key difference: break leaves the loop, while continue only skips one pass.

Skipping Even Numbers

A common use of continue is filtering. We skip values we do not care about.

This loop prints only odd numbers by continuing whenever a number is even.

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 8; i++) {
            if (i % 2 == 0) {
                continue;
            }
            System.out.println(i);
        }
    }
}

break in a while Loop

break and continue work in every loop type: for, while, and do-while.

This while loop counts up but breaks out the moment the total passes 10.

public class Main {
    public static void main(String[] args) {
        int total = 0;
        int n = 1;
        while (true) {
            total += n;
            if (total > 10) {
                break;
            }
            n++;
        }
        System.out.println("Stopped at total = " + total);
    }
}

break Only Exits One Loop

An important rule: a plain break exits only the innermost loop that surrounds it.

If you have a loop inside a loop, break leaves the inner one. The outer loop keeps running.

Watch this nested example carefully.

public class Main {
    public static void main(String[] args) {
        for (int row = 1; row <= 3; row++) {
            for (int col = 1; col <= 3; col++) {
                if (col == 2) {
                    break;
                }
                System.out.println("row " + row + ", col " + col);
            }
        }
    }
}

Nested break Result

The break only stopped the inner column loop, so each row printed just col 1.

The outer row loop continued for all three rows.

To escape both loops at once you need a labeled break, which is the next lesson.

continue Affects the Nearest Loop Too

Just like break, a plain continue only affects the loop directly around it.

In nested loops, continue jumps to the next iteration of the inner loop, not the outer one.

Keep this in mind when your logic spans two loops.

public class Main {
    public static void main(String[] args) {
        for (int a = 1; a <= 2; a++) {
            for (int b = 1; b <= 3; b++) {
                if (b == 2) {
                    continue;
                }
                System.out.println(a + "-" + b);
            }
        }
    }
}

Quick Mental Model

Picture a loop as a track you run around.

break = leave the track and walk away. continue = skip ahead to the start line for the next lap.

Both only act on the track you are currently on, never the one outside it.

Quick Check

Test your understanding of break vs continue.

Recap

You learned that break exits the nearest loop and continue skips to its next iteration.

In nested loops, both only affect the innermost loop. To control an outer loop you need labels, which you will explore next.

Frequently asked questions

Is the “break and continue” lesson free?

Yes — the full text of “break and continue” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “break and continue”?

Skip and exit iterations. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “break and continue” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. break and continue
  2. Labeled break
  3. Labeled continue
  4. Avoiding Spaghetti Flow
← Back to Java Academy