break的用法
当运行到17行时我以为会跳出整个7-35行的for语句,但是却跑到7行,继续运行for语句,为什么?break不是会跳出整个for语句吗?
public class LabeledFor {
public static void main(String[] args) {
int i = 0;
outer: // Can't have statements here
for(; true ;) { // infinite loop
inner: // Can't have statements here
7.for(; i < 10; i++) {
System.out.println("i = " + i);
if(i == 2) {
System.out.println("continue");
continue;
}
if(i == 3) {
System.out.println("break");
i++; // Otherwise i never
// gets incremented.
17.break;
}
if(i == 7) {
System.out.println("continue outer");
i++; // Otherwise i never
// gets incremented.
continue outer;
}
if(i == 8) {
System.out.println("break outer");
break outer;
}
for(int k = 0; k < 5; k++) {
if(k == 3) {
System.out.println("continue inner");
continue inner;
}
}
35}
}
}
}
------解决方案--------------------break确实已经跳出整个7-35行的for语句了,但是看你外层的for语句,for(; true ;)
这样会不会有又重新运行了7到35的语句了呢?
------解决方案--------------------break是跳出本次循环,因为你用的是双重循环,所以它还会继续下一个循环啊