break

1.break

break is used to completely end a loop (the loop in which the break is located) and jump out of the loop body to execute the statements following the loop .

public static void main(String []args) {
        for (int i=0; i<3; i++) {
            System.out.println();
            System.out.print("   i=" + i);
            System.out.println();
            for (int j=0; j<5; j++) {
                System.out.print("   j=" + j);
                if (2 == j) {
                    break;
                }
            }
        }
    }

结果:
i=0
d=0 d=1 d=2
i=1
d=0 d=1 d=2
i=2
d=0 d=1 d=2

break label:

Jump out of the loop marked by the label directly and execute the following code;

public static void main(String []args) {
        label1:
        for (int i=0; i<3; i++) {
            System.out.println();
            System.out.print("   i=" + i);
            System.out.println();
            for (int j=0; j<5; j++) {
                System.out.print("   j=" + j);
                if (2 == j) {
                    break label1;
                }
            }
        }
        System.out.println();
        System.out.println("   直接跳出这个label标记的for循环");
    }

Result:
i=0
j=0 j=1 j=2
directly jump out of the for loop marked by this label

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325703309&siteId=291194637