1 /*
2 三种循环的区别。
3
4 1. 如果条件判断从来没有满足过,那么for循环和while循环将会执行0次,但是do-while循环会执行至少一次。
5 2. for循环的变量在小括号当中定义,只有循环内部才可以使用。while循环和do-while循环初始化语句本来就在外面,所以出来循环之后还可以继续使用。
6 */
7 public class Demo13LoopDifference {
8 public static void main(String[] args) {
9 for (int i = 1; i < 0; i++) {
10 System.out.println("Hello");
11 }
12 // System.out.println(i); // 这一行是错误写法!因为变量i定义在for循环小括号内,只有for循环自己才能用。
13 System.out.println("================");
14
15 int i = 1;
16 do {
17 System.out.println("World");
18 i++;
19 } while (i < 0);
20 // 现在已经超出了do-while循环的范围,我们仍然可以使用变量i
21 System.out.println(i); // 2
22 }
23 }