在Java中,continue
關(guān)鍵字有以下幾種用法:
continue
關(guān)鍵字時,跳過當(dāng)前迭代,繼續(xù)下一次循環(huán)的執(zhí)行。for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
輸出結(jié)果為:
1
3
5
7
9
continue
可以指定跳過的是哪個循環(huán)。for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j % 2 == 0) {
continue;
}
System.out.println("i = " + i + ", j = " + j);
}
}
輸出結(jié)果為:
i = 0, j = 1
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
i = 4, j = 1
continue
還可以用于結(jié)束當(dāng)前循環(huán),轉(zhuǎn)而執(zhí)行下一個循環(huán)。for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
輸出結(jié)果為:
0
1
2
4