do while 和 while 区别

 1 package com.ibeve.demo;
 2 
 3 public class WhileDemo {
 4     public static void main(String[] args) {
 5 
 6         /**
 7          * 定义初始化表达式; while(条件表达式){ 循环体(执行语句) }
 8          */
 9         int x = 1;
10         // while (x < 13) {
11         // System.out.println("x=" + x);
12         // x = x + 2;
13         // }
14 
15         do {
16             System.out.println("do : x=" + x);
17             x++;
18         } while (x < 0);
19 
20         int y = 1;
21         while (y < 0) {
22             System.out.println("y=" + y);
23             y++;
24         }
25 
26         /**
27          * while:先判断条件,只有条件满足才执行循环体。
28          * do while:先执行循环体,再判断条件,条件满足,再继续执行循环体。
29          * 简单一句话:do while 无论条件是否满足,循环体至少执行一次。
30          */
31     }
32 }

猜你喜欢

转载自www.cnblogs.com/believeus/p/8952620.html