Overlay For Door Escape Problem

Asked 1 years ago, Updated 1 years ago, 102 views

    for (Type type : types) {
        for (Type t : types2) {
             if (some condition) {
                 // // Do something and break...
                 break; // Breaks out of the inner loop
             }
        }
    }

What do I do if I want to completely break out of this overlapping for statement?

loops java

2022-09-21 23:15

1 Answers

If you write the label on break, it's

    public class Test {
      public static void main(String[] args) {
        outerloop:
        for (int i=0; i < 5; i++) {
                  for (int j=0; j < 5; j++) {
                            if (i * j > 6) {
                                      System.out.println("Breaking");
                                      break outerloop;
                            }
                     System.out.println(i + " " + j);
                              }
                  }
        System.out.println("Done");
        }
    }

Specify the label outerloop: before the conditional statementWest When break outerloop; is encountered, it leaves the repeat statement where the label starts. In the code above,

    outerloop:
        for (int i=0; i < 5; i++) {
                  for (int j=0; j < 5; j++) {
                            if (i * j > 6) {
                                      System.out.println("Breaking");
                                      break outerloop;
                            }
                    System.out.println(i + " " + j);
              }        

This iteration statement where outerloop started ends

    System.out.println("Done");

This is going to work.


2022-09-21 23:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.