How to stop a loop in Java


This tutorial will help you understand the different ways on how to stop a loop in Java with examples.

What is a loop?

A loop is a type of control structure that helps us to execute a code multiple times until it meets a certain condition. There are different types of loop controls in Java for example for loop, do-while loop, and while loop.

Why stop a loop?

There may be certain situations where we want to exit the loop and continue with the execution of the remaining code. For example, we may want to execute a loop 10 times, but at the same time, we want to exit the loop after 5 iterations. In this case, we need some method to stop the loop execution. For this, we can use the break statement within the loop.

In the below sections, we will see the various ways on how to stop a loop in Java.

Break statement in Java

Break statement structure

Below is the syntax of using the break statement inside a loop. To know more about using the break statement in different types of loops, please refer here.

jump-statement
break;

We can use the break statement to stop the execution of the loop and proceed with the execution of the statements following the loop. Below is the flow chart that shows how the break statement works in Java.

How to stop a loop in Java

How to stop a for loop in Java using break

The below example shows how to stop a for loop in Java using the break statement. In this example, we terminate the for loop when the value of the variable is 3. It then executes the statement after the loop.

public class stopLoop{

  public static void main(String[] args) {
    for(int i=0;i<=8;i++) {
      if(i==3) {
        System.out.println("Exit for loop when the value of i is 3");
        break;
      }
        
      System.out.println("i: " + i);
    }
    System.out.println("Outside loop");
  }

}
i: 0
i: 1
i: 2
Exit for loop when the value of i is 3
Outside loop

Labeled break to stop a loop

Another approach to stopping a loop is to use the labeled break. This is useful when we have a nested loop. A loop within another loop is called a nested loop. In some cases, we may want to break both the loops which are the outer loop and the inner loop. We can achieve this by using the labeled break.

In the below example, we have provided labels for the outer loop as loop1 and inner loop as loop2. When the condition i=2 and j=2 are satisfied. it exits the entire loop execution which means it stops the execution of both the inner and outer loop. It then proceeds with the execution of statements after the outer for loop.

The below code shows you how to stop a loop in Java using the labeled break.

public class JavabreakLabeledFor {

  public static void main(String[] args) {
    loop1:
    for(int i=1;i<=3;i++) {
      loop2:
      for(int j=1;j<=3;j++) {
        if(i==2 && j==2) {
          System.out.println("Exit labeled  inner loop");
          break loop1;
        }
        System.out.println("i:" + i + " " + "j:" + j);
      }
    }
    System.out.println("Outside loop");
  }

}
i:1 j:1
i:1 j:2
i:1 j:3
i:2 j:1
Exit labeled  inner loop
Outside loop

Using return statement to stop a loop

We can also use the return statement instead of the break keyword. In this case, we can stop the loop execution by returning some value.

In this example, when the value of the variable reaches 1, it returns the value that is present in the String text. In this way, it stops the loop execution

The below code shows you how to stop a loop in Java using the return statement.

public class stopLoopDemo {

  public static void main(String[] args) {
    String result = getResult();
    System.out.println(result);

  }
  
  public static String getResult() {
    String text = "Result ";
    for(int i=0;i<5;i++) {
      text = text + i;
      if(i==1) {
        return text;
      }
         
    }
    return "fail";
  }

}
Result 01

 

Reference