All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Break For Loop In Java

Last Updated : Mar 11, 2024

How To Break For Loop In Java

In this article we will show you the solution of how to break for loop in java, the for loop is a powerful Java construct that can iterate over a collection of elements or execute a series of instructions repeatedly.

You may, however, need to exit the loop before all iterations are complete.

In this article, we will look at various ways to break out of a for loop in Java, equipping you with the knowledge and tools to deal with such situations efficiently.

Step By Step Guide On How To Break For Loop In Java :-

In Java, there are two basic methods for breaking a for loop

  1. Using the 'break' statement: This statement allows you to terminate the loop and go to the next sentence after the loop.
  2. Using a Boolean flag: You can break out of the loop depending on a specified condition by setting a Boolean flag to indicate the desired termination condition.

Method 1 - Using the 'break' statement

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Using 'break' statement to terminate the loop
    }
    System.out.println("Iteration: " + i);
}
  1. We start a for loop with an iteration variable 'i' set to 0.
  2. As long as 'i' is less than 10, the loop will run infinitely.
  3. We check inside the loop to determine if 'i' equals 5.
  4. If the condition is met, the loop ends with a "break" command.
  5. The loop is ended, and the program carries on to the next sentence after the loop.

Method 2 - Using a Boolean flag

boolean condition = true;
for (int i = 0; i < 10 && condition; i++) {
    if (i == 5) {
        condition = false; // Using a boolean flag to break the loop
    }
    System.out.println("Iteration: " + i);
}
  1. Using a Boolean flag: - We set the value of a Boolean variable called 'condition' to true.
  2. In the same way that we did in the last example, we set up a for loop with the iteration variable 'i' set to 0.
  3. The loop will run indefinitely if 'i' is less than 10 and 'condition' is true.
  4. Within the loop, we determine whether 'i' equals 5.
  5. If the condition is met, set the variable 'condition' to 'false' to end the loop.
  6. The loop is completed, and the program moves on to the following sentence.

Conclusion :-

We looked at two methods for breaking a for loop in Java in this tutorial.

You can quickly end the loop and go on to the next statement by using the 'break' statement.

Alternatively, you may use a Boolean flag to specify a specific termination condition and break out of the loop accordingly.

Both strategies provide flexibility and control over loop execution, allowing you to handle a variety of cases successfully.

Remember to select the correct procedure based on your individual needs.

When you want to quit the loop based on a specified condition, regardless of the loop's present state, the 'break' statement is especially handy.

Using a Boolean flag, on the other hand, enables for more sophisticated termination conditions that may be dependent on the loop's progress or external factors.

You can develop more efficient and dynamic Java code that caters to a wide range of scenarios by learning and utilizing these approaches.

Experiment with breaking for loops in Java and use this information to improve your programming skills and problem-solving talents.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪