一,循环控制
1,break:强行终止循环,用于完全结束一个循环,跳出循环体。一旦遇到break系统将结束该循环,开始执行之后的代码。
2,continue:忽略本次循环剩下的语句,接着开始执行下一次循环。
3,return:不是专门用于结束循环的,是结束一个方法,当一个方法执行到return语句时,这个方法将被结束。方法结束了循环自然就结束了。
例:
public static void main(String[] args) {
for (int i=0;i<10;i++){
if (i == 3){
break;
}
System.out.print(i+" ");
}
System.out.println("hello world");
}
将输出:0 1 2 hello world
public static void main(String[] args) {
for (int i=0;i<10;i++){
if (i == 3){
continue;
}
System.out.print(i+" ");
}
System.out.println("hello world");
}
将输出:0 1 2 4 5 6 7 8 9 hello world
public static void main(String[] args) {
for (int i=0;i<10;i++){
if (i == 3){
return;
}
System.out.print(i+" ");
}
System.out.println("hello world");
}
将输出:0 1 2
本人菜鸡一枚,若有错误之处,欢迎大家指正批评。