image.png

    1. package com.itheima.loop;
    2. public class WhileDemo4 {
    3. public static void main(String[] args) {
    4. // 目标:学会使用while循环,并理解他的流程
    5. int i = 0;
    6. while (i<3){ // while(条件表达式){代码...} 当条件表达式满足时,会执行下面语句
    7. System.out.println("HelloWorld"); // 输出3次
    8. i++;//不写i++会一直执行下去,因为i<3一直满足
    9. }
    10. // 死循环
    11. // while (true){
    12. // System.out.println("HelloWorld");
    13. // }
    14. // 还有一种
    15. int j =0;
    16. while (j<3){
    17. System.out.println("1111");
    18. }
    19. }
    20. }

    —————————什么时候用for,什么时候用while—————————
    image.png