for 和 while 区别

 1 package com.ibeve.demo;
 2 
 3 public class ForDemo {
 4 
 5     public static void main(String[] args) {
 6 
 7         /**
 8          * for(初始化表达式;循环表达式;循环后的操作表达式){ 执行语句; } 注意:条件一满足,马上走循环体,走完循环体,在走 x++
 9          */
10         for (int x = 0; x < 3; x++) {
11             System.out.println("x=" + x);
12         }
13         //x 的作用域的问题
14         //System.out.println("x=====" + x);
15     
16         int y = 0;
17         while (y < 3) {
18             System.out.println("y=" + y);
19             y++;
20         }
21         System.out.println("y=====" + y);
22         /**
23          * 1.变量有自己的作用域,对于 for 来讲,如果将用于控制循环的增量定义在 for 语句中。
24          * 那么该变量只在 for 语句内有效,for 语句执行完毕,该变量在内存中被释放。
25          * 
26          * 3. for 和 while 可以进行互换,如果需要定义循环增量,用 for 更为合适。
27          * 
28          * 总结:
29          * 什么时候使用循环结构?
30          * 当要对某些语句执行很多次时,就使用循环结构。
31          */
32     }
33 
34 }
原文地址:https://www.cnblogs.com/believeus/p/8952718.html