+ 2

Please explain the effect of break statement inside the nested loops

Java

7th Feb 2018, 2:59 PM
shivam srivastava
shivam srivastava - avatar
2 ответов
+ 14
break; will take u outside of loop in which it is placed //example 👇 public class Program { public static void main(String[] args) { while(true){ while(true) {System.out.println("b");break;} System.out.println("a"); } } } //output : b a b a b a . . .
7th Feb 2018, 3:38 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
/* If you break in an inner loop it goes to the outer. If you want to break from the outermost loop from an inner loop, then you can use labels. */ public class Program { public static void main(String[] args) { OUTER: for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { System.out.printf("i(%d) j(%d)\n", i, j); if (i == 3 && j == 3) break OUTER; } } } }
7th Feb 2018, 3:38 PM
Boris Batinkov
Boris Batinkov - avatar