In this section, we will explore the break and continue statements in Java, which are used to control the flow of loops. These statements provide a way to exit a loop or skip the current iteration, respectively.

  1. Break Statement

The break statement is used to exit from a loop or switch statement before it has completed its normal sequence. When a break statement is encountered inside a loop, the loop is immediately terminated, and the program control resumes at the next statement following the loop.

Syntax

break;

Example

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);
        }
        System.out.println("Loop terminated.");
    }
}

Explanation

  • The loop starts with i equal to 1 and increments i by 1 in each iteration.
  • When i becomes 5, the break statement is executed, and the loop is terminated.
  • The program then prints "Loop terminated."

Output

1
2
3
4
Loop terminated.

  1. Continue Statement

The continue statement is used to skip the current iteration of a loop and proceed to the next iteration. When a continue statement is encountered, the remaining code inside the loop for the current iteration is skipped, and the loop proceeds with the next iteration.

Syntax

continue;

Example

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

Explanation

  • The loop starts with i equal to 1 and increments i by 1 in each iteration.
  • When i becomes 5, the continue statement is executed, and the current iteration is skipped.
  • The program then proceeds with the next iteration, printing the numbers except for 5.

Output

1
2
3
4
6
7
8
9
10
Loop completed.

  1. Practical Exercises

Exercise 1: Using Break

Write a program that finds the first number divisible by 7 between 1 and 100 and exits the loop once it finds the number.

Solution

public class FindDivisibleBy7 {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if (i % 7 == 0) {
                System.out.println("First number divisible by 7 is: " + i);
                break;
            }
        }
    }
}

Exercise 2: Using Continue

Write a program that prints all numbers from 1 to 20, but skips the numbers that are divisible by 3.

Solution

public class SkipDivisibleBy3 {
    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            if (i % 3 == 0) {
                continue;
            }
            System.out.println(i);
        }
    }
}

  1. Common Mistakes and Tips

Common Mistakes

  • Using break and continue incorrectly: Ensure you understand the difference between break and continue. break exits the loop, while continue skips the current iteration.
  • Infinite loops: Be cautious when using break and continue in loops to avoid creating infinite loops.

Tips

  • Use break to exit a loop early when a certain condition is met.
  • Use continue to skip specific iterations of a loop based on a condition.

Conclusion

In this section, we learned about the break and continue statements in Java. The break statement is used to exit a loop prematurely, while the continue statement is used to skip the current iteration and proceed to the next one. Understanding these control flow statements is essential for writing efficient and effective loops in Java. In the next section, we will delve into the concepts of Object-Oriented Programming (OOP) in Java.

Java Programming Course

Module 1: Introduction to Java

Module 2: Control Flow

Module 3: Object-Oriented Programming

Module 4: Advanced Object-Oriented Programming

Module 5: Data Structures and Collections

Module 6: Exception Handling

Module 7: File I/O

Module 8: Multithreading and Concurrency

Module 9: Networking

Module 10: Advanced Topics

Module 11: Java Frameworks and Libraries

Module 12: Building Real-World Applications

© Copyright 2024. All rights reserved