• java 循环结构语句


    文章目录

    一、for循环

    1、for循环语句格式

    for(初始化语句;判断条件语句;控制条件语句) {

    ​ 循环体语句;

    }

    2、执行流程

    A:执行初始化语句

    B:执行判断条件语句,看其结果是true还是false

    如果是false,循环结束。

    如果是true,继续执行。

    C:执行循环体语句

    D:执行控制条件语句

    E:回到B继续

    3、for循环的执行流程图

    image-20211203152759982

    案例一

    1. package com.itheima_04;
    2. /*
    3. * for循环语句格式:
    4. * for(初始化语句;判断条件语句;控制条件语句) {
    5. * 循环体语句;
    6. * }
    7. *
    8. * 执行流程:
    9. * A:执行初始化语句
    10. * B:执行判断条件语句,看结果是true还是false
    11. * 如果是true,就继续执行
    12. * 如果是false,就结束循环
    13. * C:执行循环体语句
    14. * D:执行控制条件语句
    15. * E:回到B继续
    16. *
    17. * 需求:
    18. * 在控制台输出10次”HelloWorld”的案例。
    19. */
    20. public class ForDemo {
    21. public static void main(String[] args) {
    22. //原始写法
    23. System.out.println("HelloWorld");
    24. System.out.println("HelloWorld");
    25. System.out.println("HelloWorld");
    26. System.out.println("HelloWorld");
    27. System.out.println("HelloWorld");
    28. System.out.println("HelloWorld");
    29. System.out.println("HelloWorld");
    30. System.out.println("HelloWorld");
    31. System.out.println("HelloWorld");
    32. System.out.println("HelloWorld");
    33. System.out.println("-------------------------");
    34. //用循环改进
    35. for(int x=1; x<=10; x++) {
    36. System.out.println("HelloWorld");
    37. }
    38. }
    39. }

    4、for循环实现获取指定范围数据

    1. package com.itheima_04;
    2. /*
    3. * 需求:获取数据1-5和5-1
    4. */
    5. public class ForTest {
    6. public static void main(String[] args) {
    7. //原始做法
    8. System.out.println(1);
    9. System.out.println(2);
    10. System.out.println(3);
    11. System.out.println(4);
    12. System.out.println(5);
    13. System.out.println("-------------");
    14. //用循环改进
    15. for(int x=1; x<=5; x++) {
    16. System.out.println(x);
    17. }
    18. System.out.println("-------------");
    19. //1-5的数据我们获取到了,如何获取5-1呢?
    20. for(int x=5; x>=1; x--){
    21. System.out.println(x);
    22. }
    23. }
    24. }

    5、for循环实现1-5之间数据求和

    1. package com.itheima_04;
    2. /*
    3. * 需求:求出1-5之间数据之和
    4. *
    5. * 分析:
    6. * A:定义求和变量,初始化值是0
    7. * B:获取1-5之间的数据,用for循环实现
    8. * C:把每一次获取到的数据,累加起来就可以了
    9. * D:输出求和变量即可
    10. */
    11. public class ForTest2 {
    12. public static void main(String[] args) {
    13. //定义求和变量,初始化值是0
    14. int sum = 0;
    15. //获取1-5之间的数据,用for循环实现
    16. for(int x=1; x<=5; x++) {
    17. //把每一次获取到的数据,累加起来就可以了
    18. //sum = sum + x;
    19. /*
    20. * 第一次:sum = 0 + 1 = 1
    21. * 第二次:sum = 1 + 2 = 3
    22. * 第三次:sum = 3 + 3 = 6
    23. * 第四次:sum = 6 + 4 = 10
    24. * 第五次:sum = 10 + 5 = 15
    25. */
    26. sum += x;
    27. }
    28. //输出求和结果
    29. System.out.println("sum:"+sum);
    30. }
    31. }

    6、for循环实现1-100之间偶数和

    1. package com.itheima_04;
    2. /*
    3. * 需求:求出1-100之间偶数和
    4. *
    5. * 分析:
    6. * A:定义求和变量,初始化值是0
    7. * B:获取1-100之间的数据,用for循环实现
    8. * C:把获取到的数据进行判断,看是否是偶数
    9. * 如果是,就累加
    10. * D:输出求和结果
    11. */
    12. public class ForTest3 {
    13. public static void main(String[] args) {
    14. //定义求和变量,初始化值是0
    15. int sum = 0;
    16. //获取1-100之间的数据,用for循环实现
    17. for(int x=1; x<=100; x++) {
    18. //把获取到的数据进行判断,看是否是偶数
    19. if(x%2 ==0) {
    20. sum += x;
    21. }
    22. }
    23. //输出求和结果
    24. System.out.println("sum:"+sum);
    25. }
    26. }

    7、for循环实现在控制台打印水仙花数

    1. package com.itheima_04;
    2. /*
    3. * 需求:在控制台输出所有的”水仙花数”
    4. *
    5. * 分析:
    6. * 什么是水仙花数呢?
    7. * 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
    8. * 举例:153就是一个水仙花数。
    9. * 153 = 1*1*1 + 5*5*5 + 3*3*3
    10. *
    11. * A:三位数其实就告诉了我们水仙花数的范围
    12. * 100-999
    13. * B:如何获取一个数据的每一个位上的数呢?
    14. * 举例:我有一个数据153,请问如何获取到个位,十位,百位
    15. * 个位:153%10 = 3;
    16. * 十位:153/10%10 = 5;
    17. * 百位:153/10/10%10 = 1;
    18. * 千位:...
    19. * 万位:...
    20. * C:让每个位上的立方和相加,并和该数据进行比较,如果相等,就说明该数据是水仙花数,在控制台输出
    21. */
    22. public class ForTest4 {
    23. public static void main(String[] args) {
    24. //通过循环获取到每一个三位数
    25. for(int x=100; x<1000; x++) {
    26. //获取个位,十位,百位
    27. int ge = x%10;
    28. int shi = x/10%10;
    29. int bai = x/10/10%10;
    30. //让每个位上的立方和相加,并和该数据进行比较,如果相等,就说明该数据是水仙花数,在控制台输出
    31. if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x) {
    32. System.out.println(x);
    33. }
    34. }
    35. }
    36. }

    8、for循环实现统计水仙花的个数

    1. package com.itheima_04;
    2. /*
    3. * 需求:统计”水仙花数”共有多少个
    4. *
    5. * 分析:
    6. * A:定义统计变量,初始化值是0
    7. * B:获取三位数,用for循环实现
    8. * C:获取三位数的个位,十位,百位
    9. * D:判断这个三位数是否是水仙花数,如果是,统计变量++
    10. * E:输出统计结果就可以了
    11. */
    12. public class ForTest5 {
    13. public static void main(String[] args) {
    14. //定义统计变量,初始化值是0
    15. int count = 0;
    16. //获取三位数,用for循环实现
    17. for(int x=100; x<1000; x++) {
    18. //获取三位数的个位,十位,百位
    19. int ge = x%10;
    20. int shi = x/10%10;
    21. int bai = x/10/10%10;
    22. //判断这个三位数是否是水仙花数,如果是,统计变量++
    23. if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x) {
    24. count++;
    25. }
    26. }
    27. //输出统计结果就可以了
    28. System.out.println("水仙花数共有:"+count+"个");
    29. }
    30. }

    二、while循环

    1、while循环语句格式

    基本格式

    while(判断条件语句) {

    ​ 循环体语句;

    }

    扩展格式

    初始化语句;

    while(判断条件语句) {

    ​ 循环体语句;

    ​ 控制条件语句;

    }

    2、执行流程图

    image-20211203153404085

    案例二

    1. package com.itheima_05;
    2. /*
    3. * while循环语句的基本格式:
    4. * while(判断条件语句) {
    5. * 循环体语句;
    6. * }
    7. * 扩展格式:
    8. * 初始化语句;
    9. * while(判断条件语句) {
    10. * 循环体语句;
    11. * 控制条件语句;
    12. * }
    13. *
    14. * 回顾for循环的语句格式:
    15. * for(初始化语句;判断条件语句;控制条件语句) {
    16. * 循环体语句;
    17. * }
    18. */
    19. public class WhileDemo {
    20. public static void main(String[] args) {
    21. //输出10次HelloWorld
    22. /*
    23. for(int x=1; x<=10; x++) {
    24. System.out.println("HellloWorld");
    25. }
    26. */
    27. //while循环实现
    28. int x=1;
    29. while(x<=10) {
    30. System.out.println("HellloWorld");
    31. x++;
    32. }
    33. }
    34. }

    3、while循环实现1-100之间数据求和

    1. package com.itheima_05;
    2. /*
    3. * 求1-100之和。
    4. * 练习:统计水仙花个数。
    5. */
    6. public class WhileTest {
    7. public static void main(String[] args) {
    8. //回顾for循环实现
    9. /*
    10. //定义求和变量
    11. int sum = 0;
    12. //获取1-100之间的数据
    13. for(int x=1; x<=100; x++) {
    14. //累加
    15. sum += x;
    16. }
    17. System.out.println("1-100的和是:"+sum);
    18. */
    19. //while循环实现
    20. //定义求和变量
    21. int sum = 0;
    22. int x = 1;
    23. while(x<=100) {
    24. sum += x;
    25. x++;
    26. }
    27. System.out.println("1-100的和是:"+sum);
    28. }
    29. }

    三、do…while循环

    1、do…while循环语句格式

    基本格式

    do {

    ​ 循环体语句;

    }while((判断条件语句);

    扩展格式

    初始化语句;

    do {

    ​ 循环体语句;

    ​ 控制条件语句;

    } while((判断条件语句);

    2、执行流程图

    image-20211203154011999

    案例三

    1. package com.itheima_06;
    2. /*
    3. * do...while循环的基本格式:
    4. * do {
    5. * 循环体语句;
    6. * }while(判断条件语句);
    7. * 扩展格式:
    8. * 初始化语句;
    9. * do {
    10. * 循环体语句;
    11. * 控制条件语句;
    12. * }while(判断条件语句);
    13. * 执行流程:
    14. * A:执行初始化语句;
    15. * B:执行循环体语句;
    16. * C:执行控制条件语句;
    17. * D:执行判断条件语句,看是true还是false
    18. * 如果是true,回到B继续
    19. * 如果是false,就结束
    20. *
    21. * 练习:
    22. * 求和案例
    23. * 统计水仙花个数
    24. */
    25. public class DoWhileDemo {
    26. public static void main(String[] args) {
    27. //输出10次 HelloWorld
    28. /*
    29. for(int x=1; x<=10; x++) {
    30. System.out.println("HelloWorld");
    31. }
    32. */
    33. //do...while改写
    34. int x=1;
    35. do {
    36. System.out.println("HelloWorld");
    37. x++;
    38. }while(x<=10);
    39. }
    40. }

    四、三种循环的区别

    虽然可以完成同样的功能,但是还是有小区别:

    do…while循环至少会执行一次循环体。

    for循环和while循环只有在条件成立的时候才会去执行循环体

    for循环语句和while循环语句的小区别:

    使用区别:控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继续使用,如果你想继续使用,就用while,否则推荐使用for。原因是for循环结束,该变量就从内存中消失,能够提高内存的使用效率。

    案例四

    1. package com.itheima_06;
    2. /*
    3. * 三种循环的区别:
    4. * A:do...while至少执行一次循环体
    5. * B:for,while循环先判断条件是否成立,然后决定是否执行循环体
    6. *
    7. * for和while的小区别:
    8. * for循环的初始化变量,在循环结束后,不可以被访问。而while循环的初始化变量,是可以被继续使用的。
    9. * 如果初始化变量,后面还要继续访问,就使用while,否则,推荐使用for。
    10. *
    11. * 循环的使用推荐:
    12. * for -- while -- do...while
    13. */
    14. public class DoWhileDemo2 {
    15. public static void main(String[] args) {
    16. /*
    17. int x = 3;
    18. while(x<3) {
    19. System.out.println("我爱林青霞");
    20. x++;
    21. }
    22. System.out.println("--------------");
    23. int y = 3;
    24. do {
    25. System.out.println("我爱林青霞");
    26. y++;
    27. }while(y<3);
    28. */
    29. for(int x=1; x<=10; x++){
    30. System.out.println("爱生活,爱Java");
    31. }
    32. //这里的x无法继续访问
    33. //System.out.println(x);
    34. System.out.println("-----------------");
    35. int y = 1;
    36. while(y<=10) {
    37. System.out.println("爱生活,爱Java");
    38. y++;
    39. }
    40. System.out.println(y);
    41. }
    42. }
  • 相关阅读:
    php之phpstorm自动代码补全的使用
    基于ssm的机场网上订票系统设计与实现-计算机毕业设计源码+LW文档
    webrtc学习--了解webrtc服务器
    这可能是Android组件化最完美的形态吧?
    自适应控制——仿真实验二 用Narendra方案设计模型参考自适应系统
    java 企业工程管理系统软件源码 自主研发 工程行业适用
    “第六十二天”
    Node.js中child_process模块概要
    com.sun.tools.javac.code.TypeTags解决办法
    【C++ • STL】一文带你走进string
  • 原文地址:https://blog.csdn.net/qq_51916951/article/details/126327401