Java Programming Handbook

    Java Continue and Break Statement

    In Java, loops are often used to execute a block of code repeatedly. However, sometimes we need to skip certain iterations or stop the loop entirely based on a specific condition. This is where the continue and break statements come into play. They help control the flow of the loop.

    In this blog, we will explore how to use the continue and break statements, understand their purpose, and see how they work in different types of loops.

    What is the continue Statement?#

    The continue statement in Java is used to skip the current iteration of a loop and continue with the next iteration. When the continue statement is encountered inside a loop, it forces the loop to move directly to the next iteration without executing the remaining code in the current iteration.

    Syntax of continue Statement#

    continue;

    The continue statement can only be used inside loops (for, while, do-while). When encountered, it skips the remaining code inside the loop for the current iteration and jumps to the next iteration of the loop.

    Example 1: Using continue in a for Loop#

    Let's see how the continue statement works in a for loop.

    class Main { public static void main(String[] args) { // Looping from 1 to 10 for (int i = 1; i <= 10; i++) { // Skip even numbers if (i % 2 == 0) { continue; // Skip the rest of the loop body for even numbers } System.out.println(i); // Print odd numbers } } }

    Explanation:

    • The if (i % 2 == 0) condition checks if the number is even.
    • If the number is even, the continue statement is triggered, and the System.out.println(i); is skipped for that iteration.
    • As a result, only odd numbers (1, 3, 5, 7, 9) are printed.

    Expected Output:

    1 3 5 7 9

    What is the break Statement?#

    The break statement in Java is used to terminate the execution of the loop immediately, regardless of the loop's condition. When the break statement is executed inside a loop, it exits the loop and continues with the next statement after the loop.

    Syntax of break Statement#

    break;

    The break statement can be used in any loop (for, while, do-while) and in switch statements to exit early.

    Example 2: Using break in a for Loop#

    Let's see how the break statement works in a for loop.

    class Main { public static void main(String[] args) { // Looping from 1 to 10 for (int i = 1; i <= 10; i++) { if (i == 6) { break; // Exit the loop when i equals 6 } System.out.println(i); // Print numbers from 1 to 5 } } }

    Explanation:

    • The loop starts from 1 and prints numbers.
    • When i equals 6, the break statement is triggered, and the loop is immediately exited.
    • The loop terminates before printing numbers 6 to 10.

    Expected Output:

    1 2 3 4 5

    Using continue and break in a while Loop#

    Both the continue and break statements can also be used in while loops. Let’s see some examples.

    Example 3: Using continue in a while Loop#

    class Main { public static void main(String[] args) { int i = 1; // Looping from 1 to 10 while (i <= 10) { if (i % 2 == 0) { i++; // Increment the value of i before continuing continue; // Skip even numbers } System.out.println(i); // Print odd numbers i++; } } }

    Explanation:

    • The continue statement skips the printing of even numbers.
    • The loop prints only odd numbers from 1 to 9.

    Expected Output:

    1 3 5 7 9

    Example 4: Using break in a while Loop#

    class Main { public static void main(String[] args) { int i = 1; // Looping from 1 to 10 while (i <= 10) { if (i == 6) { break; // Exit the loop when i equals 6 } System.out.println(i); // Print numbers from 1 to 5 i++; } } }

    Explanation:

    • The loop prints numbers from 1 to 5.
    • The break statement is triggered when i equals 6, and the loop is terminated.

    Expected Output:

    1 2 3 4 5

    continue and break with Nested Loops#

    In Java, you can also use continue and break in nested loops (loops inside loops). By default, these statements affect only the innermost loop. However, you can use labeled continue and break to control outer loops as well.

    Example 5: Using continue in Nested Loops#

    class Main { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { continue; // Skip the iteration where i == 2 and j == 2 } System.out.println("i = " + i + ", j = " + j); } } } }

    Explanation:

    • The outer loop iterates over i, and the inner loop iterates over j.
    • The continue statement skips the iteration when both i equals 2 and j equals 2.
    • The loop will print all pairs of i and j, except when i == 2 and j == 2.

    Expected Output:

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

    Example 6: Using break in Nested Loops#

    class Main { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { break; // Exit the inner loop when i == 2 and j == 2 } System.out.println("i = " + i + ", j = " + j); } } } }

    Explanation:

    • The break statement exits the inner loop when i == 2 and j == 2.
    • The inner loop terminates early, and the outer loop continues its next iteration.

    Expected Output:

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

    Conclusion#

    In this blog, we have learned:

    • The continue statement skips the current iteration of a loop and moves to the next one.
    • The break statement immediately exits the loop and continues with the next statement after the loop.
    • We explored how to use continue and break in different types of loops (for, while) and with nested loops.
    • These control statements are powerful tools for managing the flow of loops and making your code more efficient and readable.

    Last updated on Apr 09, 2025