Search code examples
javawhile-loopbreak

Break statement inside two while loops


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.


Solution

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