Break statement inside two while loops

adrian picture adrian · Sep 12, 2012 · Viewed 88.2k times · Source

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.

Answer

Abhishekkumar picture Abhishekkumar · Sep 12, 2012

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