Break statement inside two while loops

后端 未结 11 1243
时光取名叫无心
时光取名叫无心 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:40

    while (a) {
    
       while (b) {
    
          if (b == 10) {
              break;
          }
       }
    }
    

    In the above code you will break the inner most loop where (ie. immediate loop) where break is used.

    You can break both the loops at once using the break with label

    label1: 
    while (a) {
    
       while (b) {
    
          if (b == 10) {
              break label1;
          }
       }
    }
    

提交回复
热议问题