Search This Blog

Java: break out nested loops

  • Method 1: Use label:
    
        whileloop: {
          while(true) {
            for(String s : strings) {
              if("DONE".equalsIgnoreCase(s)){
                break whileloop;
              }
            }
          }
        }
  • Method 2: Use a boolean tag:
    
        boolean done = false;
        while(!done) {
          for(String s : strings) {
            if("DONE".equalsIgnoreCase(s)){
              done = true;
              break;
            }
          }
        }



See also

No comments:

Post a Comment