Break statement inside two while loops

后端 未结 11 1244
时光取名叫无心
时光取名叫无心 2020-12-24 14:00

Let\'s say I have this:

while(a){

  while(b){

   if(b == 10)
     break;
 }
}

Question: Will the break statement take m

11条回答
  •  余生分开走
    2020-12-24 14:49

    It will break only the most immediate while loop. Using a label you can break out of both loops: take a look at this example taken from here


    public class Test {
      public static void main(String[] args) {
        outerloop:
        for (int i=0; i < 5; i++) {
          for (int j=0; j < 5; j++) {
            if (i * j > 6) {
              System.out.println("Breaking");
              break outerloop;
            }
            System.out.println(i + " " + j);
          }
        }
        System.out.println("Done");
      }
    }
    

提交回复
热议问题