Break statement inside two while loops

狂风中的少年 提交于 2019-11-30 01:58:24
Abhishekkumar

In your example break statement will take you out of while(b) loop

while(a) {

   while(b) {

      if(b == 10) {
         break;
      }
   }  
   // break will take you here.
}
Ivan Koblik

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");
  }
}
Jigar Joshi

Only from the inner one. Use labeled break if you wish to break to specific loop

label1:
for(){
  label2:
  for(){
      if(condition1)
      break label1;//break outerloop

      if(condition2)
      break label2;//break innerloop
  }
}

Also See

Kumar Vivek Mitra
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;
      }
   }
}

It will break out of the loop that immediately encloses it.

You can however, break to a label:

myLabel:

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

I don't generally like to use this pattern because it easily leads to spaghetti code. Use an unlabeled break or a flag to terminate your loop.

Hignesh Hirani

@Abhishekkumar

Break keyword has it's derived root from C and Assembly, and Break it's sole purpose to passes control out of the compound statement i.e. Loop, Condition, Method or Procedures.

Please refer these...

http://tigcc.ticalc.org/doc/keywords.html#break

http://www.functionx.com/cpp/keywords/break.htm

http://en.wikipedia.org/wiki/Break_statement#Early_exit_from_loops

So, if you want to get out of Two loops at same time then you've to use two Breaks, i.e. one in inner loop and one in outer loop.

But you want to stop both loop at same time then you must have to use exit or return.

A break statement will take you out of the innermost loop enclosing that break statement.

In the example the inner while loop.

As a curious note, in PHP the break statement accept a numeric parameter which tells how many outer loops you want to break, like this:

$i = 0;
while (++$i) {
   switch ($i) {
      case 5:
         echo "At 5<br />\n";
         break 1;  /* Exit only the switch. */
      case 10:
         echo "At 10; quitting<br />\n";
         break 2;  /* Exit the switch and the while. */
      default:
         break;
  }
}

The java break statement won't take you out of multiple nested loops.

Only the inner loop of course.

You can raise a flag to pass the information to the outer while loop. In this case the information can be stored in a variable breakBothLoopsFlag and the outer while loop acts according to this information. See pseudo code below:

int breakBothLoopsFlag = 0;

while(a){

  while(b){
      if(b == 10) {
        breakBothLoopsFlag = 1;
        break;
      }
    }

   if(breakBothLoopsFlag == 1) {
     break;
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!