二者的作用和区别
1. break:直接跳出当前循环体(while、for、do while)或程序块(switch)。其中switch case执行时,一定会先进行匹配,匹配成功返回当前case的值,再根据是否有break,判断是否继续输出,或是跳出判断(可参考switch的介绍)。
2. continue:不再执行循环体中continue语句之后的代码,直接进行下一次循环。
代码演示
-
public
class Test {
-
public static void main(String[] args) {
-
System.out.println(
"-------continue测试----------");
-
for (
int i =
0; i <
5; i++) {
-
if (i ==
2) {
-
System.out.println(
"跳过下面输出语句,返回for循环");
-
continue;
-
}
-
-
System.out.println(i);
-
}
-
-
-
System.out.println(
"----------break测试----------");
-
for (
int i =
0; i <
5; i++) {
-
if (i ==
2) {
-
System.out.println(
"跳过下面输出语句,返回for循环");
-
break;
-
}
-
System.out.println(i);
-
}
-
-
}
-
}
运行结果:
可以看到测试 continue时,当 i==3,直接跳过了continue之后的输出语句,进入下一次循环。
在break测试中,当 i==2,直接跳出了 for 循环,不再执行之后的循环