小计:
1:break跳出单层循环
2:使用带标签的语句可以跳出多层循环
3:continue:跳出一次循环,进行下一次循环;
public class ControlJump {
public static void main(String[] args){
for(int x=0;x<4;x++){
/*
while(x==2){
break;
}
用while的时候的break不能跳出for循环,因为break只能跳出单层循环,而while就是一层循环
*/
if(x==2){
break;
}
System.out.println("Hallo Word");
}
//使用带标签的语句跳出多层循环
wc:for(int x=0;x<3;x++){
nc:for(int y=0;y<4;y++){
if(y==2){
break wc;
}
System.out.print("*");
}
System.out.print("555");
}
System.out.print("\n");
for(int x=1;x<5;x++){
if(x==3){
System.out.println("程序即将结束运行");
continue;
}
System.out.println("x="+x);
}
}
}
程序运行结果
Hallo Word
Hallo Word
**
x=1
x=2
程序即将结束运行
x=4
一个易错点:
for(int x=0;x<4;x++){
while(x==2){
break;
}
System.out.println("Hallo Word");
}
这里不能使用while,因为用while的时候的break不能跳出for循环,break只能跳出单层循环,而while就是一层循环