public class LabLoop{
public static void main(String argv[]){
LabLoop ml = new LabLoop();
ml.amethod();
}
public void amethod(){
outer:
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
if(j>1)
//Try this with break instead of continue
break outer;
System.out.println("i "+ i + " j "+j);
}
}//End of outer for
System.out.println("Continuing");
}
}
输出是:i 0 j 0
i 0 j 1
为什么是这样的输出??能不能给我详细的解释下 它的循环过程?? 在线等 辛苦大家了啊!!!
public static void main(String argv[]){
LabLoop ml = new LabLoop();
ml.amethod();
}
public void amethod(){
outer:
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
if(j>1)
//Try this with break instead of continue
break outer;
System.out.println("i "+ i + " j "+j);
}
}//End of outer for
System.out.println("Continuing");
}
}
输出是:i 0 j 0
i 0 j 1
为什么是这样的输出??能不能给我详细的解释下 它的循环过程?? 在线等 辛苦大家了啊!!!
j > 1的时候循环结束,跳到outer标签
本来break只会跳出内层循环
这里设了个标签就可以随便跳了
i 0 j 0
i 0 j 1
Continuingbreak语句的含义您应该知道吧就是结束循环(也就是说不再继续循环了)。
而本程序是带标签的break。它会结束标签位置下方的循环。所以当j=2时。直接从内循环的for跳了出来
同时也结束了外循环
http://blog.csdn.net/myjava_024/archive/2008/11/21/3347898.aspx
public static void main(String argv[]) {
LabLoop ml = new LabLoop();
ml.amethod();
} public void amethod() {
outer: for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (j > 1)
//当j>1时,跳出outer标记的for循环,其它的没什么好解释
break outer;
System.out.println("i " + i + " j " + j);
}
}// End of outer for
System.out.println("Continuing");
}
}
break outer 后面的程序
则打印出下面的结果:
i 0 j 0
i 0 j 1
i 1 j 0
i 1 j 1
Continuing