Java学习之路:流程控制( 三 )

【while和do while的区别】

  • while先判断后执行,do while是先执行后判断
  • do while总是保证循环至少被执行一次
public class DoWhileDemo01 {public static void main(String[] args) {int i = 0;int sum = 0;do {sum = sum + i;i++;}while (i<=100);System.out.println(sum);}}结果:
50504.3 for循环for循环可以让程序变得更简单 。
public class ForDemo01 {public static void main(String[] args) {int a = 0; //初始化条件while (a<=100){ //条件判断System.out.println(a); //循环体a+=10; //迭代条件}System.out.println("while循环结束");for (int i=0;i<=100;i+=10){System.out.println(i);}System.out.println("for循环结束");}}结果:
0102030405060708090100while循环结束0102030405060708090100for循环结束【练习】
① 计算0到100之间的奇数和偶数的和 。
public class ForDemo02 {public static void main(String[] args) {//计算0到100之间的奇数和偶数的和int oddsum = 0;int evensum = 0;for(int i = 0;i <= 100;i+=1){if (i%2!=0){oddsum += i;}else{evensum += i;}}System.out.println("奇数的和:"+oddsum);System.out.println("偶数的和:"+evensum);}}结果:
奇数的和:2500偶数的和:2550② 用while或for循环输出1-1000之间能被5整除的数,并且每行输出8个 。
public class Test01 {public static void main(String[] args) {//用while或for循环输出1-1000之间能被5整除的数,并且每行输出8个 。int i = 1;while (i <= 1000){double a = i % 5;if (a == 0){if (i % 8 == 0){System.out.println(i+" ");}else{System.out.print(i+" ");}}i++;}}}结果:
5 10 15 20 25 30 35 4045 50 55 60 65 70 75 8085 90 95 100 105 110 115 120125 130 135 140 145 150 155 160165 170 175 180 185 190 195 200205 210 215 220 225 230 235 240245 250 255 260 265 270 275 280285 290 295 300 305 310 315 320325 330 335 340 345 350 355 360365 370 375 380 385 390 395 400405 410 415 420 425 430 435 440445 450 455 460 465 470 475 480485 490 495 500 505 510 515 520525 530 535 540 545 550 555 560565 570 575 580 585 590 595 600605 610 615 620 625 630 635 640645 650 655 660 665 670 675 680685 690 695 700 705 710 715 720725 730 735 740 745 750 755 760765 770 775 780 785 790 795 800805 810 815 820 825 830 835 840845 850 855 860 865 870 875 880885 890 895 900 905 910 915 920925 930 935 940 945 950 955 960965 970 975 980 985 990 995 1000 ③ 打印九九乘法表
public class Test02 {public static void main(String[] args) {//打印九九乘法表for (int i = 1; i <= 9; i++) {for (int j = 1; j <= i; j++) {if (i==j){System.out.println(" "+i+"x"+j+"="+i*j);}else{System.out.print(" "+i+"x"+j+"="+i*j);}}}}}结果:
1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=814.4 增强for循环主要用于数组或集合 。
语法:
for(声明语句 : 表达式){//代码语句}【注意】
  • 声明新的局部变量 , 该变量的类型必须和数组元素的类型匹配;
  • 表达式是要访问的数组名,或者是返回值为数组的方法 。
public class ForDemo03 {public static void main(String[] args) {int[] num = {10,20,30,40,50}; //定义一个数组for (int i = 0;i<5;i++){System.out.println(num[i]);}System.out.println("==================");//遍历数组的元素for (int x:num){System.out.println(x);}}}结果:
1020304050==================10203040505 break和continue语句【break】
在任何循环语句的主体部分 , 均可用break控制循环的流程,可以强行退出循环 。
【continue】
用在循环语句体中,用于终止某次循环过程,即跳过循环体中未执行的语句 , 接着进行下一次是否执行循环的判定 。
【goto】
带标签的breakcontinue
本章节学习结束,接下来再接再厉,加油?。。?

推荐阅读