zoukankan      html  css  js  c++  java
  • For和while的区别

     1 package day03;
     2 
     3 public class ForWhile {
     4     /*for和while的区别:
     5     1、条件控制语句所控制的自增变量因为归属for循环的语法结构中,
     6     for循环结束以后就不能再被访问到了。
     7     2、条件控制语句所控制的自增变量对于while循环来说不归属其语法结构中,
     8     在while循环结束后,该变量还可以继续使用。
     9 
    10     * */
    11     public static void main(String[] args) {
    12         // for循环内部[定义]的变量, 在循环结束后, 就会从内存中消失
    13         for (int i = 1; i <=5; i++) {
    14             System.out.println(i);
    15         }
    16         // System.out.println(i);    //  错误: 找不到符号i
    17         System.out.println("----------");
    18         for (int i = 1; i <=10; i++) {
    19             System.out.println(i);
    20         }
    21 
    22         //while循环
    23         int a =1;
    24         while(a<=5){
    25             System.out.println(a);
    26             a++;
    27         }
    28         System.out.println(a+"---");
    29     }
    30 }

    执行结果:

    欢迎批评指正,提出问题,谢谢!
  • 相关阅读:
    【bzoj2006】超级钢琴
    【bzoj4940】这是我自己的发明
    【arc076E】Connected?
    【agc004C】AND Grid
    选举
    几何
    打击目标
    【CF Gym100228】Graph of Inversions
    【CodeChef】Chef and Graph Queries
    大包子玩游戏
  • 原文地址:https://www.cnblogs.com/xxeleanor/p/14203552.html
Copyright © 2011-2022 走看看