public class Worker { public static boolean sign(int i){ System.out.print("DoJob "); 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 ", 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 "); } } 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 标签 标注的代码块的圈。