first: for (int i = 0; i < 10; i++) { two: for (int j = 0; j < 10; j++) { if (j == 5) { break first; } System.out.print("(" + i + "," + j + ")"); } System.out.println(); }
在for循环前面定义标签后,在内循环里如果根据逻辑想要进行外层循环break或continue,就可以直接卸载break和continue后面。
输出:
(0,0)(0,1)(0,2)(0,3)(0,4)