JAVA Tutorial



BREAK & CONTINUE IN JAVA


Break & Continue in Java

In Java, break and continue are used to control the flow of loops. Both are essential for handling more complex loop behaviors and improving code efficiency.

🔵 The break Statement

The break statement is used to exit a loop (or switch statement) prematurely. When a break is encountered inside a loop, the loop terminates immediately, and the program continues with the next statement following the loop.

💡 Example: Using break in a Loop

In the following example, the loop is designed to print numbers from 1 to 10, but the loop stops if the number is 5, thanks to the break statement:

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

🔴 Output

The output of the above code will be:

1
2
3
4
  

As soon as the loop reaches 5, the break statement terminates the loop, and the program continues with the next statement after the loop.

🔵 The continue Statement

The continue statement is used to skip the current iteration of a loop and proceed to the next iteration. When the continue is encountered, the rest of the loop's code for the current iteration is skipped, and the next iteration begins.

💡 Example: Using continue in a Loop

In this example, the loop prints numbers from 1 to 10 but skips printing the number 5, thanks to the continue statement:

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

🔴 Output

The output of the above code will be:

1
2
3
4
6
7
8
9
10
  

The number 5 is skipped in the output because the continue statement skips the current iteration when i == 5.

🔴 Key Differences Between break and continue

  • break exits the loop entirely and moves to the next statement after the loop.
  • continue skips the current iteration and proceeds with the next iteration of the loop.

⚙️ Nested Loops with break and continue

Both break and continue can be used in nested loops. However, break will only break out of the innermost loop, while continue will skip the current iteration of the innermost loop.

💡 Example: Using break in Nested Loops

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

🔴 Output

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

The inner loop breaks when i == 2, and the program skips to the next iteration of the outer loop.

💡 Example: Using continue in Nested Loops

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

🔴 Output

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

🔴 Key Takeaways

  • break exits the loop completely.
  • continue skips the current iteration and continues with the next iteration.
  • Nested Loops: Both break and continue can be used in nested loops to control flow more precisely.

Practice Challenge: Write a program that prints a multiplication table for numbers 1 to 3, but skips multiples of 2 using continue and stops the entire program if it prints 4 using break.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review