java - How to break out of nested loops -


i'm making program nested loops needed search through array find spot input within array. here example:

public void placepiece(int column) {         boolean goodinput = false;          while(!goodinput) {             for(int x = 5; x >= 0; x--) {                 if(boardstatus[x][column] == 0) {                      setrow(x);                     boardstatus[x][column] = 1;                     goodinput = true;                     break;                  }else if(boardstatus[0][column] == 1) {                      goodinput = false;                     break;                 }else{                  }              }         }     } 

the method takes parameter column in piece should located (received mouse listener). if column in 2d array filled top, program gets stuck in endless loop within "else if", , i'm unsure how break out of loop. how break out of loop if there bad input user can try give column input.

an easy way use labeled break statement.

mylabelname: for(outer loop)     for(inner loop)         if(condition)             break mylabelname; 

this useful when you'd rather not waste time iterating on other objects/items when you've found needed.


to expand, when use break; (without label) exit parent loop. ex:

mylabelname: for(outer loop){     for(inner loop){         if(condition){             break mylabelname; //breaks outer loop         }         else if(other condition){             break; //breaks parent (inner/nested) loop         }      }  } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -