java中 label 配合 break continue 使用的其实是比较少的.
这种做法在业务代码中比较少见.
在读其他库的代码时可能会遇到该语法的使用.
下面对该语法做简短解释.
break label
用来终止循环体的循环,使用break 可以终止的循环体包括三种, for,while,do-while
不带 label使用 break 时
执行 break ,会终止离 break 关键字最近的循环体的循环.
示例如下
for (int i = 0; i < 3; i++) {
System.out.println(String.format("start outer for loop index %d", i));
int j = 0;
while (true) {
System.out.println(String.format("inner loop with while index %d", j));
if (j == 2) {
//终止最近的for,while,do-while循环
break;
} else {
j++;
}
}
System.out.println(String.format("end outer for loop index %d", i));
}
结果为
start outer for loop index 0
inner loop with while index 0
inner loop with while index 1
inner loop with while index 2
end outer for loop index 0
start outer for loop index 1
inner loop with while index 0
inner loop with while index 1
inner loop with while index 2
end outer for loop index 1
start outer for loop index 2
inner loop with while index 0
inner loop with while index 1
inner loop with while index 2
end outer for loop index 2
通过打印可以看出,内部循环每次执行到2时就将当前 while 死循环停止.
而外部循环则会从0遍历到2,正常循环遍历.不受 break 的影响.
带 label 的break
会终止指定 label 所对应的循环体.
示例为从二维的student age 数组中,找第一个年纪为9岁的学生.
int students[][] = {{10, 12, 11}, {8, 9, 1}, {9, 15, 7}};
jumpOutHere:
for (int i = 0; i < 3; i++) {
System.out.println(String.format("start outer for loop index %d", i));
int j = 0;
while (j < 3) {
System.out.println(String.format("current retrieve value %d", students[i][j]));
if (students[i][j] == 9) {
break jumpOutHere;
} else {
j++;
}
}
System.out.println(String.format("end outer for loop index %d", i));
}
结果为
start outer for loop index 0
current retrieve value 10
current retrieve value 12
current retrieve value 11
end outer for loop index 0
start outer for loop index 1
current retrieve value 8
current retrieve value 9
当遍历到二维数组中第一个年纪为9的学生时,直接终止 label 对应的循环.此处为外层循环.
continue label
用来跳过循环体中本次执行的循环,使用continue 可以跳过的循环体包括三种, for,while,do-while
不带 label 的 continue
会跳过距离continue最近的循环体的本次执行,执行下一次循环
for (int i = 0; i < 3; i++) {
System.out.println(String.format("start outer for loop index %d", i));
for(int k = 0;k<3;k++){
if(k==1)
continue;
System.out.println(String.format("inner loop index %d",k));
}
System.out.println(String.format("end outer for loop index %d", i));
}
start outer for loop index 0
inner loop index 0
inner loop index 2
end outer for loop index 0
start outer for loop index 1
inner loop index 0
inner loop index 2
end outer for loop index 1
start outer for loop index 2
inner loop index 0
inner loop index 2
end outer for loop index 2
通过示例可以看出当 k=1时会跳过本次内部循环(也是离 continue 最近的循环),直接执行下一次内部循环.
带 label 的 continue
会跳过标记为 label 的循环体中的本次循环的执行,并执行, label 标记位置的下一个循环.
CONTINU_LABLE:
for (int i = 0; i < 3; i++) {
System.out.println(String.format("start outer for loop index %d", i));
for (int k = 0; k < 3; k++) {
if (k == 1)
continue CONTINU_LABLE;
System.out.println(String.format("inner loop index %d", k));
}
System.out.println(String.format("end outer for loop index %d", i));
}
start outer for loop index 0
inner loop index 0
start outer for loop index 1
inner loop index 0
start outer for loop index 2
inner loop index 0
通过结果可以看出,当 k=1时,直接就跳到CONTINU_LABLE标记的循环体的下一次循环了.
结尾
喜欢的麻烦点个赞,关注下.