Breaking out of nested loop in java


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);
         }
      }
   }
}

Updated on: 24-Feb-2020

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements