Java Break and Continue

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 17 / 50 · English

Java Break and Continue

Stop the loop, or just skip one turn.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Break and Continue.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

break exits the nearest enclosing loop or switch. continue skips the rest of the current loop iteration and proceeds according to that loop form. Trace which lines run before and after each statement.

02 / Step by step

Explore the details

Controlling Loop Flow

Sometimes you need to exit a loop early or skip one iteration. Java gives you two keywords for this: break and continue.

The break Statement

break immediately exits the entire loop. No more iterations run — execution jumps to the line after the closing brace.

The continue Statement

continue skips the rest of the current iteration and jumps straight to the next one. The loop itself keeps running.

Using Both Together

break and continue work the same way in while loops. break always exits the nearest enclosing loop, and continue always skips to the next iteration of that same loop.

Java reference fragmentJAVA
// break — exit loop early
for (int i = 1; i <= 9; i++) {
    if (i == 5) break;
    System.out.print(i + " ");  // 1 2 3 4
}

// continue — skip one iteration
for (int i = 1; i <= 9; i++) {
    if (i % 2 == 0) continue;  // skip even
    System.out.print(i + " ");  // 1 3 5 7 9
}

// Labelled break — exit outer loop
outer:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) break outer;
        System.out.println(i + "," + j);
    }
}
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        for (int n = 1; n <= 6; n++) {
            if (n == 2) continue;
            if (n == 5) break;
            System.out.println(n);
        }
    }
}
Predict first. Then reveal the expected output / வெளியீடு
1
3
4

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Move the println before both if statements. What changes?

Reveal the answer and reasoning / விடை

The values 1 through 5 are printed: each value is printed before the skip or exit decision.

One mistake to avoid: continue does not terminate the loop. In a while loop, make sure it cannot skip the update needed to finish.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.