• 21.Java中for循环


    1.格式:

    for(初始化表达式;循环条件表达式;循环后的操作表达式)
    {
            执行语句;
    }

    2.定义需求: 想要打印5次helloworld

    public static void main(String[] args) {
            for (int x = 0; x < 5; x++) {
                System.out.println("hello java");
            }
        }

    3.for的执行流程

    for 知道要进行循环,读到x=0 的时候,在内存中开辟了空间,定义变量x 赋值为0。接着进行条件判断 x<5,为真,这个时候对满足条件后执行了循环体的内容System.out.println("hello java");当循环体执行完毕之后,执行x < 5;后的表达式即 x++ 。x自增后变为了1 ,再次进行判断 x<5 (int x=0 只执行一次),如果为真就再次运行System.out.println("hello java");如果为假,for循环结束。

    2、for 和while的区别

    public static void main(String[] args) {
            for (int x = 0; x < 5; x++) {
                System.out.println("hello java");
            }
            System.out.println(x); 
            //x cannot be resolved to a variable
    
            int y = 0;
            while (y < 5) {
                System.out.println("hello world");
                y++;
            }
            System.out.println(y);
    }

      

    4. 错误

    解释 x 为什么会找不到,注意了变量的作用域,也就是变量的作用范围。x 只在 for 循环的大括号内有效,出了这个区域,就无效了.在内存中就消失了。x消失后,仍要访问它,肯定会报错的。

    y 就不一样了,y 是定义在while 外的。while循环完毕仍有效  while的初始化 动作在外边,循环结束后y 仍然存在。

    当定义的y 只作为循环增量存在的话的,循环完毕后y就没有用了,但是y还是占着一块内存。所以,如果定义的变量只作为循环增量存在的话,就用for 循环可以节约内存。

    其实for 和while 是可以互换的。

    最后总结

    1、for里面的两个表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真就执行循环体,然后再执行循环后的操作表达式,接着继续判断循环条件,重复找个过程,直到条件不满足为止。

    2、while与for可以互换,区别在于for为了循环而定义的变量在for循环结束时就在内存中释放。而while循环使用的变量在循环结束后还可以继续使用。

    3、最简单无限循环格式:while(true) , for(;;),无限循环存在的原因是并不知道循环多少次,而是根据某些条件,来控制循环。推荐使用while(true)

    while(true){
                
    }
    for(;;){
                
            }
    for(;true;){
                
            }

    for 练习:

    1. 获取1-10的和,并打印。
    2. 1-100之间 7的倍数的个数,并打印。
    public static void main(String[] args) {
            // 获取1到10的和1+2+3+4+5+6+7+8+9+10
            int sum = 0;
            for (int x = 1; x <= 10; x++) {
                System.out.println((sum + x) + "=" + sum + "+" + x);
                sum = sum + x;
            }
            System.out.println(sum);// 55
        }
    public static void main(String[] args) {
            // 1-100之间 7的倍数的个数,并打印。
            int count = 0;
            for (int x = 0; x <= 100; x++) {
                if (x % 7 == 0) {
                    System.out.println(x);
                    count++;
                }
            }
            System.out.println(count);
    }

    累加思想:通过变量记录住循环操作后的结果;通过循环的形式.进行累加的动作。

    计数器思想:通过一个变量记录住数据的状态变化,也是通过循环完成。

    循环常见错误:

    多加分号:在for括号后和循环体之间加分号是常见错误。

    错误:

    程序编译运行都可以通过,只是不是我们想要的结果。

    for(int i=0;i<100;i++);{
                System.out.println("hello ");
    }

    正确:

    for(int i=0;i<100;i++){
                System.out.println("hello ");
    }

    错误;是一个死循环

    int i=0;
    while(i<100);{
        System.out.println("hello");
            i++;
    }

    正确:

    int i=0;
    while(i<100){
        System.out.println("hello");
            i++;
    }

    语句的嵌套应用

    什么是嵌套形式,其实就是语句中还有语句。

    想要打印出矩形:

    public static void main(String[] args) {
            for (int x = 0; x < 5; x++) {
                System.out.println("******");
            }
        }

    public static void main(String[] args) {
            for (int x = 0; x < 5; x++) {
                System.out.print("*");
            }
        }

    这里用“*”表示矩形的边。

    public static void main(String[] args) {
            for (int x = 0; x < 5; x++) {
                for(int y=0;y<6;y++){
                    System.out.print("*");
                }
                System.out.println();
            }
    }

    forfor 嵌套for循环练习2

    打印此种格式的图案

    *****  

       ****

       ***

       **

       *

    public static void main(String[] args) {
            for (int x = 5; x > 0; x--) {
                for(int y=x;y>0;y--){
                    System.out.print("*");
                }
                System.out.println("");
            }
        }

    练习:

    *

    **

    ***

    ****

    *****

    public static void main(String[] args) {
            for (int x = 0; x < 5; x++) {
                for (int y = 0; y <= x; y++) {
                    System.out.print("*");
                }
                System.out.println("");
            }
    
    }

    练习:99乘法表

    public static void main(String[] args) {
            for (int x = 1; x <= 9; x++) {
                for (int y = 1; y <= x; y++) {
                    System.out.print(y + "*" + x + "=" + x * y + '	');
                }
                System.out.println(" ");
            }
    }
    author@nohert
  • 相关阅读:
    前端规范标准(一)
    node之旅(3) Express之我的第一个应用
    node之旅(2) hello wrold!
    node之旅(1) 安装NodeJS
    CSS3 基础知识
    博客转移公告
    博客主题更换留念
    网络流-费用流zkw算法
    网络流-最大流ISAP
    字符串总结-三大“自动机”
  • 原文地址:https://www.cnblogs.com/gzgBlog/p/13575020.html
Copyright © 2020-2023  润新知