Chapter 17 of 57

Break and Continue in Java

When working with loops, sometimes we don't want the loop to simply run from beginning to end. We may want to stop the loop completely or skip one particular iteration.

Java provides two useful statements for this: break and continue.

Although they are both used inside loops, they do very different things.

The break Statement

The break statement is used to immediately stop a loop.

For example, suppose we want to print numbers from 1 to 10, but we want to stop when the number reaches 5:

for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        break;
    }

    System.out.println(i);
}

Output:

1
2
3
4

When i becomes 5, the if condition becomes true and break is executed. Java immediately exits the loop, so 5 and all the numbers after it are never printed.

Think of break as saying:

"Stop the loop right now."

break with a while Loop

break isn't limited to for loops. We can also use it with while loops.

int i = 1;

while (i <= 10) {

    if (i == 5) {
        break;
    }

    System.out.println(i);
    i++;
}

The output is:

1
2
3
4

Again, as soon as i reaches 5, the loop stops.

The continue Statement

The continue statement works differently.

Instead of stopping the entire loop, continue skips the current iteration and moves to the next one.

For example:

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    System.out.println(i);
}

Output:

1
2
4
5

When i is 3, continue is executed. Java skips the rest of that iteration, so System.out.println(i) is not executed for 3.

But the loop itself does not stop. It continues with 4 and then 5.

Think of continue as saying:

"Skip this one and move to the next iteration."

break vs continue

The difference is much easier to understand with a simple example.

Suppose we have:

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        break;
    }

    System.out.println(i);
}

Output:

1
2

The loop completely stops when it reaches 3.

Now change break to continue:

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    System.out.println(i);
}

Output:

1
2
4
5

This time, only 3 is skipped. The loop continues running.

So remember:

Statement

What it does

break

Stops the entire loop

continue

Skips the current iteration

A Real-World Example

Imagine you are processing a list of students.

Suppose you want to skip students who were absent and continue processing the others. continue can be useful for this kind of situation.

For example:

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    System.out.println("Processing student " + i);
}

Student 3 is skipped, but the remaining students are still processed.

On the other hand, if you encounter a situation where you know there is no reason to continue processing anything else, break can stop the loop completely.

break and continue in Nested Loops

When loops are placed inside other loops, break and continue affect the innermost loop they are used in.

For example:

for (int i = 1; i <= 3; i++) {

    for (int j = 1; j <= 3; j++) {

        if (j == 2) {
            break;
        }

        System.out.println(j);
    }
}

Here, break stops the inner loop when j becomes 2. The outer loop can still continue.

Nested loops can become more complicated, so it's important to remember which loop the break or continue belongs to.

When Should You Use Them?

Use break when you have a reason to stop the loop completely.

For example, you are searching for something and have already found it. There is no need to continue searching.

Use continue when you want to ignore the current item but still process the remaining items.

For example, you may want to skip invalid data and continue processing the rest.

Both statements are simple, but they are very useful when you need more control over how a loop behaves.