break和continue的详细使用程序(Thinking in Java第147-149页源程序)
public class example_147{
public static void main(String args[]){
int i = 0;
outer:
for(;true;){
inner:
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++;
break ; //跳出inner循环进入outer的for循环,进入for(true)后i=8重新进入inner开始循环。
//此时break跳过了i++.
}
if(i == 7){
System.out.println("continue outer");
i++;
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;
}
}
}
}
System.out.println("Over!");
}
}
public class example_149{
public static void main(String args[]){
int i = 0;
outer:
while(true){
System.out.println("outer while loop");
while(true){
i++;
System.out.println("i = " + i);
if(i == 1){
System.out.println("continue");
continue;
}
if(i == 3){
System.out.println("continue outer");
continue outer;
}
if(i == 5){
System.out.println("break");
break;
}
if(i == 7){
System.out.println("break outer");
break outer;
}
}
}
}
}