In Java, we can break/exit from the current loop with the break statement. But what if we want to break an outer loop from a nested loop? In Java we can name our loops using labels. By using labels we can specify which loop we would like to break out of (also called “breaking to the label”).

For example, in the code below we label the outer loop flag_loop. In the nested loop we are evaluating values in an array. If we find a value we don’t like, we will break the outer flag_loop.

for(int i=0; i<pd.size(); i++){
  String[] pdi = pd.get(1).split(delimiter);

  for(int j=0; j<pdi.length; j++){
    if(pdi[j].length() == 0 && j != 6) {
      flag = false;
      break flag_loop;

Leave a Reply

How to Break from Nested Loop in Java