java lable使用

    public void test() {
        int i = 0;
        retry:
        for (; ; ) {
            System.out.println("start == >>");
            for (; ; ) {
                i++;
                System.out.println("i = " + i);
                if (i == 3) {
                    System.out.println(" continue retry; == >>" + i);
                    //continue;     //注释1
                    continue retry;  //注释2
                }
                if (i == 7) {
                    System.out.println(" break retry; == >>" + i);
                    //break; //注释3
                    break retry; //注释4
                }

            }
        }
        System.out.println("end");
    }```

结论:
- 开启注释2,注释4
`start == >>
i = 1
i = 2
i = 3
 continue retry; == >>3
start == >>
i = 4
i = 5
i = 6
i = 7
 break retry; == >>7
end`
- 开启注释1,注释4
`
start == >>
i = 1
i = 2
i = 3
 continue retry; == >>3
i = 4
i = 5
i = 6
i = 7
 break retry; == >>7
end
`
- 开启注释3,则死循环。

break retry 相当于是跳出多层循环,并不在执行该循环。
continue retry 相当于是跳转到label处继续执行这个循环。相当于c中goto,只不过java中label必须在循环前面。

你可能感兴趣的:(java lable使用)