do{ //代码语句 }while(布尔表达式);
-
while先判断后执行,do...while 先执行后判断
-
package com.kuangshen.struct; public class DoWhileDemo01 { public static void main(String[] args) { int sum = 0; int i = 0; do { sum = sum + i; i++; }while(i<=100); System.out.println(sum); } }
package com.kuangshen.struct; public class DoWhileDemo02 { public static void main(String[] args) { int a = 0; while (a<0){ System.out.println(a); } System.out.println("======================================"); do { System.out.println(a); }while (a<0); } }