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: 2020-02-24T12:17:34+05:30

412 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements