JAVA中break和continue的区别

continue时,跳出本次循环,继续执行下次循环。(continue语句的作用是中断当前的这次循环,继续后面的循环。)

提示:continue的作用与break类似,主要用于循环,所不同的是break会结束程序块的执行,
而continue只会结束其之后程序块的语句,并跳回循环程序块的开头继续下一个循环,而不是离开循环。

  

For(int i = 0;i<=5;i++){
    If(i = =3)continue;
System.out.println("The number is:"+i);
   }
结果为:
 The number is:0
 The number is:1
 The number is:2
 The number is:4
 The number is:5

 

break时,跳出循环(结束循环),执行下面的语句。(break语句是结束这次循环,不再执行该循环块或者程序块)

提示:break可以离开当前switch、for、while、do while的程序块,并前进至程序块后下一条语句,
在switch中主要用来中断下一个case的比较。在for、while与do while中,主要用于中断目前的循环执行。

  

 For(int i = 0;i<=6;i++){
    If(i = =3)break;
System.out.println("The number is:"+i);
   }
结果为:
 The number is:0
 The number is:1
 The number is:2 

 

posted @ 2021-03-03 08:36  被梦想尘封的时光  阅读(453)  评论(0编辑  收藏  举报