Breaking out of nested loop in java

Java 8Object Oriented ProgrammingProgramming

Yes, break statement can be used in nested for loops. It works on the for loop in which it is applied. See the example below.

Example

public class Tester {
   public static void main(String[] args) {
      for(int i = 0; i< 2; i++) {
         for(int j = 0; j < 2; j++){
            if(i == 1) {
               break;
            }
            System.out.println("i = " + i+",j = " + j);
         }
      }
   }
}
raja
Published on 06-Feb-2018 11:53:40
Advertisements