再探java基础——break和continue的用法
break
break可用于循环和switch...case...语句中。
用于switch...case中:
执行完满足case条件的内容内后结束switch,不执行下面的语句。
eg:
public static void breakSwitch1() { int n = 1; switch (n) { case 1: System.out.println("this is one."); break; case 2: System.out.println("this is two."); break; default: System.out.println("Others."); } }
结果:
this is one.
eg2:
public static void breakSwitch2() { int n = 1; switch (n) { case 1: System.out.println("this is one."); //break; case 2: System.out.println("this is two."); break; default: System.out.println("Others."); } }
结果:
this is one.
this is two.
用于循环中
break; 结束本层循环。
eg:
public static void breakTest1() { System.out.println("begin to circulating."); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 2 && j==3) { System.out.println("break is executed!"); break; } System.out.print(i + "*" + j + "=" + i * j + " "); } System.out.println(); } }
结果:
begin to circulating.
0*0=00*1=00*2=00*3=00*4=0
1*0=01*1=11*2=21*3=31*4=4
2*0=02*1=22*2=4break is executed!
3*0=03*1=33*2=63*3=93*4=12
4*0=04*1=44*2=84*3=124*4=16
break [flag]; 结束带有[flag]标记层到本层的所有循环。
eg1:
public static void breakTest2() { System.out.println("begin to circulating."); loop:for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 2 && j==3) { System.out.println("break is executed!"); break loop; } System.out.print(i + "*" + j + "=" + i * j + " "); } System.out.println(); } }
结果:
begin to circulating.
0*0=00*1=00*2=00*3=00*4=0
1*0=01*1=11*2=21*3=31*4=4
2*0=02*1=22*2=4break is executed!
eg2:
public static void breakTest3() { loop: for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print("level " + i + ":"); for (int k = 0; k < 5; k++) { if (i == 1 && j == 2 && k == 3) { break loop; } System.out.print(i + "+" + j + "+" + k + "=" + i + j + k + " "); } System.out.println(" "); } System.out.println(); } }
结果
level 0:0+0+0=0000+0+1=0010+0+2=0020+0+3=0030+0+4=004
level 0:0+1+0=0100+1+1=0110+1+2=0120+1+3=0130+1+4=014
level 0:0+2+0=0200+2+1=0210+2+2=0220+2+3=0230+2+4=024
level 0:0+3+0=0300+3+1=0310+3+2=0320+3+3=0330+3+4=034
level 0:0+4+0=0400+4+1=0410+4+2=0420+4+3=0430+4+4=044
level 1:1+0+0=1001+0+1=1011+0+2=1021+0+3=1031+0+4=104
level 1:1+1+0=1101+1+1=1111+1+2=1121+1+3=1131+1+4=114
level 1:1+2+0=1201+2+1=1211+2+2=122
continue
continue; 结束本层的本次循环。
eg1:
public static void continueTest1() { for (int i = 0; i < 10; i++) { if (i == 6) { continue; } System.out.print(i + " "); } }
结果:
012345789
eg2:
public static void continueTest2() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 2 && j == 3) { continue; } System.out.print(i + "*" + j + "=" + i * j + " "); } System.out.println(); } }
结果:
0*0=00*1=00*2=00*3=00*4=0
1*0=01*1=11*2=21*3=31*4=4
2*0=02*1=22*2=42*4=8
3*0=03*1=33*2=63*3=93*4=12
4*0=04*1=44*2=84*3=124*4=16
continue[flag]; 结束带有[flag]标记层的本次循环,且终止[flag]层以内的层在[flag]标记层本次循环下的循环。有点绕口,也很难表述清楚,直接看例子吧:
eg1:
public static void continueTest3() { loop: for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == 3 && j == 4) { continue loop; } System.out.print(i + "*" + j + "=" + i * j + " "); } System.out.println(); } }
结果
0*0=00*1=00*2=00*3=00*4=00*5=00*6=00*7=00*8=00*9=0
1*0=01*1=11*2=21*3=31*4=41*5=51*6=61*7=71*8=81*9=9
2*0=02*1=22*2=42*3=62*4=82*5=102*6=122*7=142*8=162*9=18
3*0=03*1=33*2=63*3=94*0=04*1=44*2=84*3=124*4=164*5=204*6=244*7=284*8=324*9=36
5*0=05*1=55*2=105*3=155*4=205*5=255*6=305*7=355*8=405*9=45
6*0=06*1=66*2=126*3=186*4=246*5=306*6=366*7=426*8=486*9=54
7*0=07*1=77*2=147*3=217*4=287*5=357*6=427*7=497*8=567*9=63
8*0=08*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=648*9=72
9*0=09*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
eg2:
public static void continueTest4() { loop: for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print("level " + i + ":"); for (int k = 0; k < 5; k++) { if (i == 1 && j == 2 && k == 3) { continue loop; } System.out.print(i + "+" + j + "+" + k + "=" + i + j + k + " "); } System.out.println(" "); } System.out.println(); } }
结果:
level 0:0+0+0=0000+0+1=0010+0+2=0020+0+3=0030+0+4=004
level 0:0+1+0=0100+1+1=0110+1+2=0120+1+3=0130+1+4=014
level 0:0+2+0=0200+2+1=0210+2+2=0220+2+3=0230+2+4=024
level 0:0+3+0=0300+3+1=0310+3+2=0320+3+3=0330+3+4=034
level 0:0+4+0=0400+4+1=0410+4+2=0420+4+3=0430+4+4=044
level 1:1+0+0=1001+0+1=1011+0+2=1021+0+3=1031+0+4=104
level 1:1+1+0=1101+1+1=1111+1+2=1121+1+3=1131+1+4=114
level 1:1+2+0=1201+2+1=1211+2+2=122level 2:2+0+0=2002+0+1=2012+0+2=2022+0+3=2032+0+4=204
level 2:2+1+0=2102+1+1=2112+1+2=2122+1+3=2132+1+4=214
level 2:2+2+0=2202+2+1=2212+2+2=2222+2+3=2232+2+4=224
level 2:2+3+0=2302+3+1=2312+3+2=2322+3+3=2332+3+4=234
level 2:2+4+0=2402+4+1=2412+4+2=2422+4+3=2432+4+4=244
level 3:3+0+0=3003+0+1=3013+0+2=3023+0+3=3033+0+4=304
level 3:3+1+0=3103+1+1=3113+1+2=3123+1+3=3133+1+4=314
level 3:3+2+0=3203+2+1=3213+2+2=3223+2+3=3233+2+4=324
level 3:3+3+0=3303+3+1=3313+3+2=3323+3+3=3333+3+4=334
level 3:3+4+0=3403+4+1=3413+4+2=3423+4+3=3433+4+4=344
level 4:4+0+0=4004+0+1=4014+0+2=4024+0+3=4034+0+4=404
level 4:4+1+0=4104+1+1=4114+1+2=4124+1+3=4134+1+4=414
level 4:4+2+0=4204+2+1=4214+2+2=4224+2+3=4234+2+4=424
level 4:4+3+0=4304+3+1=4314+3+2=4324+3+3=4334+3+4=434
level 4:4+4+0=4404+4+1=4414+4+2=4424+4+3=4434+4+4=444
PS
突然记得我以前用C语言写过类似的程序,并传到了百度库中。后来找来看了一下,发现写的好简单,但当时硬是写了大半天,而且写完之后还特别高兴,觉得这就是我的杰作,并传到了百度文库中……那时是刚上大学,在读大一刚入门的时候写的。程序员就是这样,总是沉醉于自己的作品之中,那怕是很简单,只要是在进步,把自己不会的东西写出来了,就特别高兴,这也是我们不断努力的动力啊!
有几个例子还是挺有意思的,拿出来秀一下:
例1:输入一批考试分数,用-1作为结束标志,若输入大于100, 则提示重新输入。然后计算最高分、最低分和平均值。
#include<stdio.h> void main() { int mark,n=0,sum=0,max=0,min=100; float average; for(;;) { scanf("%d",&mark); //输入学生成绩 if(mark>100) //如果输入的成绩大于100,则重新输入 { printf("Please reinput: "); continue; //结束本次循环,返回for循环 } if(mark==-1) //-1表示输入学生成绩结束 break; //终止整个循环,跳出循环体 n++; sum=sum+mark; if(mark>max) max=mark; //max存放最大的成绩 if(mark<min) min=mark; //min存放最小的成绩 } average=(float)sum/n; //“(float)sum”是强制型类型变换,使average的值一定为实数 printf("max=%d,min=%d,average=%f ",max,min,average); }
例2:计算半径从1到20时圆的面积,直到面积大于200为止。
#include<stdio.h> #define PI 3.14159265 void main() { int r; float s; for(r=1;r<=20;r++) { s=PI*r*r; if(s>200) break; printf("r=%d,s=%.2f ",r,s); } }
例3:输出50到150之间不能被5整除的整数。
#include<stdio.h> void main() { int i; for(i=50;i<=150;i++) { if(i%5==0) { printf(" "); //使输出的显示每五个数换一行。 continue; } printf("%5d",i); } printf(" "); }