32

Let's say I have this:

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

Question: Will the break statement take me out of both loops or only from the inner one? Thank you.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
adrian
  • 4,574
  • 17
  • 68
  • 119
  • If you want to break out from two nested while loop, you can use labels... https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto – Ivandro Jao Oct 11 '20 at 09:39

11 Answers11

38

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.
}
j_v_wow_d
  • 511
  • 2
  • 11
Abhishekkumar
  • 1,102
  • 8
  • 24
  • 6
    What if I want also to break while-A? – oneofakind Jun 19 '14 at 02:04
  • 1
    @oneofakind you can use one more break statement where commented line is written – Abhishekkumar Dec 19 '14 at 16:59
  • 4
    @oneofakind if you want to break out of both loops, put a label like "outer" on the first while, and then use "break outer;" to break to that level. See: https://gist.github.com/bseib/f5ee5c5baa31fa116feb715bd4037cb5 – broc.seib Oct 04 '18 at 21:08
34

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");
  }
}
Community
  • 1
  • 1
Ivan Koblik
  • 4,285
  • 1
  • 30
  • 33
12

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

RubioRic
  • 2,442
  • 4
  • 28
  • 35
jmj
  • 237,923
  • 42
  • 401
  • 438
6
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;
      }
   }
}
RubioRic
  • 2,442
  • 4
  • 28
  • 35
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
6

@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.

Hignesh Hirani
  • 1,049
  • 11
  • 16
5

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.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
2

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;
  }
}
Nelson
  • 49,283
  • 8
  • 68
  • 81
2

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 breakOuterLoopFlag and the outer while loop acts according to this information. See pseudo code below:

int breakOuterLoopFlag = 0;

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

   if(breakOuterLoopFlag == 1) {
     break;
   }
}
TechJ
  • 512
  • 2
  • 5
  • 16
1

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

In the example the inner while loop.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

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

main--
  • 3,873
  • 1
  • 16
  • 37
0

Only the inner loop of course.

Debloper
  • 158
  • 1
  • 8