8 break和continue
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
break语句详解
大家还记得switch语句吗?
switch穿透
package study.scanner;
import java.util.Scanner;
public class switchDemo02 {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
int x= scanner.nextInt();
switch (x)
{
case 3:
System.out.println("满分");
case 2:
System.out.println("冲刺");
case 1:
System.out.println("努力");
default:
System.out.println("干啥啥不行");
}
}
}
在没有使用break时,switch语句会在遇到合适的结果只会直接输出包含合适结果和在其之后的所有结果
如上题在输入3之后就会出现以下情况:
3
满分
冲刺
努力
干啥啥不行
Process finished with exit code 0
break结束穿透
很明显,我们程序是绝对不能出现这种直接将所有可能全部甩出来的结果的,我们将这种现象称作switch的穿透现象所以我们应该使用break在遇到合适结果之后直接跳出该程序,即如下所示
package study.scanner;
import java.util.Scanner;
public class switchDemo02 {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
int x= scanner.nextInt();
switch (x)
{
case 3:
System.out.println("满分");
break;
case 2:
System.out.println("冲刺");
break;
case 1:
System.out.println("努力");
break;
default:
System.out.println("干啥啥不行");
}
}
}
再输入3之后:
3
满分
Process finished with exit code 0
我们会发现加入break语句之后,在遇到了合适的case选项之后,会直接终止程序的进一步穿透,仅仅只是显示了“3”之下的相关程序处理和输出之后就跳出了switch语句,这就是break的作用;
循环中的break
break在循环中也是相同的作用,它可以在符合某些循环条件之后直接跳出循环体,并直接结束循环进入下面的流程中,由于用法和switch语句基本相同,在此不一一赘述。
只举一个例子说明一下
package study.base;
public class breakDemo01 {
public static void main(String[] args) {
int i=0;//初值赋值为0
while (i<100){
i++;//让i进行进入循环体的次数记录
if (i==30)
break;//当i=30时直接终止执行,直接跳出循环
}
System.out.println(i);
}
}
结果:
30
Process finished with exit code 0
如果没有break,那么输出的数值应该是100,而加入了break之后会使其在第30次循环后直接结束,所以输出的是30
continue语句详解
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
但由于continue只是终止了当前循环而并不会直接跳出当前循环,所以其在结束了当前循环之后还会进入下一次循环
程序详解(原始程序)
如:
package study.base;
public class continueDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
if (i%10==0){
System.out.println( );
continue;
}
System.out.print(i+" ");
}
}
}
是否continue程序对比
如果将那一段if语句直接注释掉,那么continue语句就不会发生作用,结果就会变成:
package study.base;
public class continueDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
// if (i%10==0){
// System.out.println( );
// continue;
// }
System.out.print(i+" ");
}
}
}
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Process finished with exit code 0
如果加入了if语句之后就会启用continue语句,即:
package study.base;
public class continueDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
if (i%10==0){
System.out.println( );
continue;
}
System.out.print(i+" ");
}
}
}
结果:
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
Process finished with exit code 0
可以发现的是,当加入了continue语句之后,即使在触发continue语句的前提下,也没有直接结束相关的循环语句,但跳过了在循环体中位于if语句之下的输出i+空格语句,直接回到了最上面的判断条件中。
最终对比:
为了更加清晰地辨别break和continue的区别,我们再将同一段代码利用break和continue分别运行一遍:
break:
package study.base;
public class continueDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
if (i%10==0){
System.out.println( );
break;
}
System.out.print(i+" ");
}
}
}
结果:
1 2 3 4 5 6 7 8 9
Process finished with exit code 0
continue:
package study.base;
public class continueDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
if (i%10==0){
System.out.println( );
continue;
}
System.out.print(i+" ");
}
}
}
结果:
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
Process finished with exit code 0