java中 label 配合 break continue 使用的其實(shí)是比較少的.
這種做法在業(yè)務(wù)代碼中比較少見.
在讀其他庫的代碼時可能會遇到該語法的使用.
下面對該語法做簡短解釋.
break label
用來終止循環(huán)體的循環(huán),使用break 可以終止的循環(huán)體包括三種, for,while,do-while
不帶 label使用 break 時
執(zhí)行 break ,會終止離 break 關(guān)鍵字最近的循環(huán)體的循環(huán).
示例如下
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循環(huán)
break;
} else {
j++;
}
}
System.out.println(String.format("end outer for loop index %d", i));
}
結(jié)果為
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
通過打印可以看出,內(nèi)部循環(huán)每次執(zhí)行到2時就將當(dāng)前 while 死循環(huán)停止.
而外部循環(huán)則會從0遍歷到2,正常循環(huán)遍歷.不受 break 的影響.
帶 label 的break
會終止指定 label 所對應(yīng)的循環(huán)體.
示例為從二維的student age 數(shù)組中,找第一個年紀(jì)為9歲的學(xué)生.
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));
}
結(jié)果為
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
當(dāng)遍歷到二維數(shù)組中第一個年紀(jì)為9的學(xué)生時,直接終止 label 對應(yīng)的循環(huán).此處為外層循環(huán).
continue label
用來跳過循環(huán)體中本次執(zhí)行的循環(huán),使用continue 可以跳過的循環(huán)體包括三種, for,while,do-while
不帶 label 的 continue
會跳過距離continue最近的循環(huán)體的本次執(zhí)行,執(zhí)行下一次循環(huán)
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
通過示例可以看出當(dāng) k=1時會跳過本次內(nèi)部循環(huán)(也是離 continue 最近的循環(huán)),直接執(zhí)行下一次內(nèi)部循環(huán).
帶 label 的 continue
會跳過標(biāo)記為 label 的循環(huán)體中的本次循環(huán)的執(zhí)行,并執(zhí)行, label 標(biāo)記位置的下一個循環(huán).
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
通過結(jié)果可以看出,當(dāng) k=1時,直接就跳到CONTINU_LABLE標(biāo)記的循環(huán)體的下一次循環(huán)了.
結(jié)尾
喜歡的麻煩點(diǎn)個贊,關(guān)注下.