break
跳出当前for
循环
for (int i = 0; i < 4; i++) {
if (i == 2) {
break;
}
System.out.println(i);
}
运行结果
0
1
continue
终止循环本次
操作,继续下次循环
for (int i = 0; i < 4; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
运行结果
0
1
3
continue label
给外层循环取一个别名outer
,continue outer
从标记label处继续下次循环,这里是外层
System.out.println("begin");
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
continue outer;
}
System.out.println("i=" + i + ",j=" + j);
}
}
System.out.println("over");
运行结果
begin
i=0,j=0
i=0,j=1
i=0,j=2
i=1,j=0
i=2,j=0
i=2,j=1
i=2,j=2
over
break label
给外层循环取一个别名outer
,break outer
从标记label处终止循环
System.out.println("begin");
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break outer;
}
System.out.println("i=" + i + ",j=" + j);
}
}
System.out.println("over");
运行结果
begin
i=0,j=0
i=0,j=1
i=0,j=2
i=1,j=0
over
课外分析
System.out.println("begin");
outer:
for (int i = 0; i < 3; i++) {
inner:
for (int j = 0; j < 3; j++) {
if (j == 1) {
break inner;
}
System.out.println("i=" + i + ",j=" + j);
}
}
System.out.println("over");