Java break的跳转范围

public class Worker {

    public static boolean sign(int i){
        System.out.print("DoJob\r\n");
        return i==4;
    }

    public static void main(String[] args) {
        int[] nums = {12, 23, 34, 45, 56, 67, 78, 89, 90};
        firstPoint:for (int i = 0; nums.length > i; i++) {
            System.out.printf("%d\r\n", nums[i]);
            judge: if (Worker.sign(i)){
                System.out.print("goto firstPoint");
                break firstPoint;
            }else{
                 int j = 0;
                 while (true) {
                    j++;
                    if (j == 3){
                        System.out.print("goto Judge");
                        break judge;
                    }
                }
            }
            System.out.print("ToEnd\r\n");
        }
    }

    public void testGoto() {

    }
}

运行结果:

12
DoJob
goto JudgeToEnd
23
DoJob
goto JudgeToEnd
34
DoJob
goto JudgeToEnd
45
DoJob
goto JudgeToEnd
56
DoJob
goto firstPoint  

 

从结果看出,break到judge之后,并不是跳转执行,而是跳转出圈,出哪个圈?break 标签 标注的代码块的圈。

 

posted @ 2021-03-02 18:01  华腾海神  阅读(124)  评论(0编辑  收藏  举报