Java Break / Continue


In Java, both the break and continue statements are used to control the flow of loops. They allow you to stop or skip certain iterations in a loop based on specific conditions. These statements are extremely helpful in improving code readability and managing situations where you don’t always want the loop to run completely.

The break statement is used to exit from a loop entirely, while the continue statement is used to skip the current iteration and continue with the next one. Understanding how and when to use them makes your loops more flexible and efficient.

The Break Statement

The break statement immediately terminates the current loop (for, while, or do-while) when a certain condition is met. Once the break is executed, the control jumps to the next line of code after the loop.

Syntax:

break;

This statement can appear inside any type of loop or even within a switch block.

Example: Break in a For Loop

Here’s a simple example demonstrating how break works:

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}
System.out.println("Loop ended.");

Output:

1  
2  
3  
4  
Loop ended.

In this example, the loop stops as soon as i becomes 5. The control moves directly to the line after the loop, printing “Loop ended.”

Example: Break in a While Loop

You can also use break in a while loop.

int i = 1;
while (i <= 10) {
    if (i == 6) {
        break;
    }
    System.out.println(i);
    i++;
}

Output:

1  
2  
3  
4  
5

When i equals 6, the condition triggers the break statement, and the loop ends.

Example: Break in a Nested Loop

When you have a loop inside another loop, a break statement only exits the inner loop unless it’s a labeled break.
Example:

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            break;
        }
        System.out.println("i = " + i + ", j = " + j);
    }
}

Output:

i = 1, j = 1  
i = 2, j = 1  
i = 3, j = 1

Here, the inner loop stops each time j becomes 2, but the outer loop continues with the next iteration.

Labeled Break Statement

A labeled break allows you to break out of an outer loop directly. You give a label name to the outer loop and use that label with the break statement.

Example:

outerLoop:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
            break outerLoop;
        }
        System.out.println(i + " " + j);
    }
}
System.out.println("Exited from both loops.");

Output:

1 1  
1 2  
1 3  
2 1  
Exited from both loops.

As soon as i == 2 and j == 2, the labeled break exits both loops at once.

The Continue Statement

The continue statement doesn’t stop the loop entirely; instead, it skips the current iteration and moves to the next one. It’s helpful when you want to ignore specific cases while continuing the loop.

Syntax:

continue;

Example: Continue in a For Loop

Here’s a basic example of continue in a for loop:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}

Output:

1  
2  
4  
5

The loop skips the number 3 because the continue statement sends control directly to the next iteration, skipping the print statement for that value.

Example: Continue in a While Loop

You can also use continue in a while loop:

int i = 0;
while (i < 5) {
    i++;
    if (i == 2) {
        continue;
    }
    System.out.println(i);
}

Output:

1  
3  
4  
5

When i equals 2, the continue statement skips printing it and jumps back to the next iteration.

Example: Continue in Nested Loops

Like break, continue can also be used inside nested loops, and it only affects the loop where it is placed.
Example:

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            continue;
        }
        System.out.println(i + " " + j);
    }
}

Output:

1 1  
1 3  
2 1  
2 3  
3 1  
3 3

Here, whenever j equals 2, the inner loop skips that iteration.

Labeled Continue Statement

The labeled continue works similarly to the labeled break, except it skips the current iteration of the outer loop instead of terminating it completely.

Example:

outerLoop:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            continue outerLoop;
        }
        System.out.println(i + " " + j);
    }
}

Output:

1 1  
2 1  
3 1

When j becomes 2, control jumps directly to the next iteration of the outer loop.

Difference Between Break and Continue

Here’s a quick comparison of how these two statements differ:

Feature Break Continue
Function Terminates the loop entirely Skips the current iteration
Control Flow Moves control outside the loop Moves control to the loop’s next iteration
Usage Used to stop execution early Used to skip specific cases
Works In Loops and switch statements Only in loops

Common Mistakes

  1. Using break or continue outside a loop causes a compilation error.

  2. Misusing labels can make code confusing; they should be used only when necessary.

  3. Forgetting to update loop variables after continue can lead to infinite loops.

Summary of the Tutorial

The break and continue statements are simple yet powerful tools for controlling loops in Java. Use break when you want to exit from a loop immediately, and continue when you want to skip a particular iteration but keep the loop running. With labeled versions, you can even control nested loops effectively. Mastering these statements helps you write cleaner and more efficient programs.


Practice Questions

  1. Write a Java program that prints numbers from 1 to 10 but stops completely when the number reaches 7 using a break statement.

  2. Create a Java program that prints numbers from 1 to 20 but skips all numbers that are multiples of 3 using the continue statement.

  3. Write a program that iterates through an integer array and prints the first even number. Use a break statement to exit the loop once it’s found.

  4. Write a Java program that takes user input repeatedly and counts how many positive numbers are entered before a negative number appears. Use a break statement to stop reading inputs when a negative number is entered.

  5. Use a while loop to print numbers from 1 to 10 but skip the number 5 using the continue statement.

  6. Create a nested for loop where the inner loop prints values of j from 1 to 3 for each i (1 to 3). Break out of the inner loop when j equals 2.

  7. Write a Java program using a labeled loop that prints pairs of (i, j) where both go from 1 to 3, but exits both loops completely when i and j are equal using a labeled break.

  8. Write a Java program using nested loops where the inner loop skips to the next iteration of the outer loop whenever j equals 2. Use a labeled continue statement for this.

  9. Write a program that prints numbers from 1 to 50 but stops completely when the first number divisible by 5 is found using a break statement.

  10. Write a Java program that prints odd numbers between 1 and 20 but stops the loop completely when the number becomes greater than 15 using both continue and break statements.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top