Home » Java break and continue Statements Tutorial

Java break and continue Statements Tutorial

In Java, the break and continue statements are used to control the flow of loops (for, while, do-while) and switch statements.

They provide ways to exit from a loop or skip certain iterations based on a condition.

In this tutorial, we’ll cover the usage of both break and continue statements with examples to demonstrate their behavior in different scenarios.

1. The break Statement

The break statement is used to exit a loop or a switch statement immediately. Once the break is encountered, the control jumps to the first statement outside the loop or switch block.

Syntax:

break;

Common Use Cases:

Exiting a loop early when a certain condition is met.
Exiting a switch case to avoid falling through to the next case.

Example 1: Using break in a for Loop

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

Explanation:

The for loop iterates from 1 to 10.
The break statement exits the loop when i == 5.

Output:

1
2
3
4

Example 2: Using break in a while Loop

public class BreakInWhileLoop {
    public static void main(String[] args) {
        int i = 1;

        while (i <= 10) {
            if (i == 6) {
                break;  // Exit the loop when i is 6
            }
            System.out.println(i);
            i++;
        }
    }
}

Explanation:

The while loop continues until i reaches 10.
The break statement exits the loop when i == 6.

Output:

1
2
3
4
5

Example 3: Using break in a switch Statement

public class BreakInSwitchStatement {
    public static void main(String[] args) {
        int day = 3;

        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;  // Exits the switch after this case
            case 4:
                System.out.println("Thursday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

Explanation:

The switch statement checks the value of day.
When day == 3, it prints “Wednesday” and exits the switch block using break.

Output:
Wednesday.

2. The continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration. Unlike break, it does not exit the loop but skips the remaining code inside the loop for that specific iteration.

Syntax:

continue;

Common Use Cases:

Skipping certain iterations in a loop based on a condition.

Example 4: Using continue in a for Loop

public class ContinueInForLoop {
    public static void main(String[] args) {
        // For loop example
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue;  // Skip the even numbers
            }
            System.out.println(i);
        }
    }
}

Explanation:

The for loop iterates from 1 to 10.
The continue statement skips the current iteration when i is even (i.e., i % 2 == 0).

Output:

1
3
5
7
9

Example 5: Using continue in a while Loop

public class ContinueInWhileLoop {
    public static void main(String[] args) {
        int i = 0;

        while (i < 10) {
            i++;

            if (i % 3 == 0) {
                continue;  // Skip numbers divisible by 3
            }
            System.out.println(i);
        }
    }
}

Explanation:

The while loop continues as long as i < 10.
When i is divisible by 3, the continue statement skips the print statement and moves to the next iteration.

Output:

1
2
4
5
7
8
10

Example 6: Nested Loops with continue and break

In nested loops, break and continue affect only the innermost loop where they are used.

public class NestedLoops {
    public static void main(String[] args) {
        // Outer loop
        for (int i = 1; i <= 3; i++) {
            // Inner loop
            for (int j = 1; j <= 3; j++) {
                if (i == j) {
                    continue;  // Skip when i == j
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }
    }
}

Explanation:

The outer loop runs from 1 to 3.
The inner loop also runs from 1 to 3.
The continue statement skips printing when i == j.

Output:

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

3. Breaking and Continuing in Labeled Loops

Java allows the use of labeled loops, where a label is given to a loop, and you can break or continue that specific loop from within nested loops.

Syntax:

labelName:
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (condition) {
            break labelName;  // Exits the labeled loop
        }
    }
}

Example 7: Using break with Labeled Loops

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

Explanation:

The break outerLoop exits both the inner and outer loops when i == 2 and j == 2.

Output:

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
Example 8: Using continue with Labeled Loops
public class ContinueWithLabel {
    public static void main(String[] args) {
        // Labeled outer loop
        outerLoop:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    continue outerLoop;  // Skip the inner loop and go to the next iteration of outer loop
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }
    }
}

Explanation:

The continue outerLoop skips the remaining iterations of the inner loop when i == 2 and j == 2 and moves to the next iteration of the outer loop.

Output:

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

Conclusion

The break and continue statements provide powerful control over the flow of loops in Java:

break: Used to exit a loop or switch statement immediately.
continue: Used to skip the current iteration of a loop and continue with the next iteration.
Labeled loops allow you to specify which loop to break or continue when dealing with nested loops.

By understanding these control structures, you can write more flexible and efficient code.

You may also like