Break and Continue Statements in Java


In Java, the break and continue statements are used to control the flow of loops. These statements can alter the flow of execution by skipping certain iterations or terminating loops prematurely.

1. The break Statement

The break statement is used to exit a loop or a switch-case structure prematurely. Once the break statement is encountered, the control is transferred to the code that follows the loop or switch-case.

Syntax:

    break;
        

When a break statement is executed inside a loop, the loop will stop executing, and the program will move on to the next statement after the loop.

Example 1: Using break in a for loop

Let’s see how to use the break statement in a for loop. The loop will print numbers from 1 to 5, but when the number reaches 3, it will break and stop the loop.

    public class BreakExample {
        public static void main(String[] args) {
            for (int i = 1; i <= 5; i++) {
                if (i == 3) {
                    break; // Exit the loop when i equals 3
                }
                System.out.println(i);
            }
        }
    }
        

Output:

    1
    2
        

In this example, the loop stops executing as soon as i equals 3 because the break statement is triggered.

2. The continue Statement

The continue statement is used to skip the current iteration of the loop and continue with the next iteration. Unlike break, which exits the loop entirely, continue allows the loop to keep running, but skips the rest of the code inside the loop for the current iteration.

Syntax:

    continue;
        

When a continue statement is encountered inside a loop, it skips the remaining part of the current iteration and moves on to the next iteration of the loop.

Example 2: Using continue in a for loop

Let’s see how to use the continue statement in a for loop. This loop will print all numbers from 1 to 5 except 3, which will be skipped.

    public class ContinueExample {
        public static void main(String[] args) {
            for (int i = 1; i <= 5; i++) {
                if (i == 3) {
                    continue; // Skip the iteration when i equals 3
                }
                System.out.println(i);
            }
        }
    }
        

Output:

    1
    2
    4
    5
        

In this example, when i equals 3, the continue statement is triggered, and the rest of the loop body is skipped for that iteration. The loop continues with the next iteration without printing the number 3.

3. Using break and continue in while and do-while Loops

The break and continue statements can also be used in while and do-while loops to control the flow.

Example 3: Using break in a while loop

This example demonstrates the use of the break statement in a while loop. The loop will run as long as the condition is true, but it will break when the counter reaches 3.

    public class WhileBreakExample {
        public static void main(String[] args) {
            int i = 1;
            while (i <= 5) {
                if (i == 3) {
                    break; // Exit the loop when i equals 3
                }
                System.out.println(i);
                i++;
            }
        }
    }
        

Output:

    1
    2
        

The loop terminates when i equals 3 due to the break statement.

Example 4: Using continue in a do-while loop

This example demonstrates the use of the continue statement in a do-while loop. The loop will print numbers from 1 to 5, but it will skip 3.

    public class DoWhileContinueExample {
        public static void main(String[] args) {
            int i = 1;
            do {
                if (i == 3) {
                    i++;
                    continue; // Skip the iteration when i equals 3
                }
                System.out.println(i);
                i++;
            } while (i <= 5);
        }
    }
        

Output:

    1
    2
    4
    5
        

In this example, when i equals 3, the continue statement is triggered, skipping the print statement for 3 and continuing with the next iteration of the loop.

Conclusion

The break and continue statements are powerful tools for controlling the flow of loops in Java. Use break to exit a loop prematurely and continue to skip the current iteration and move on to the next one. Both statements are useful for managing complex loop conditions and optimizing code execution.





Advertisement