do…while循环
do{
//代码语句
}while(布尔表达式)
while和do-while的区别:
- while先判断后执行。do while是是先执行后判断
- Do…while总是保证循环体会被至少执行一次!这是他们的主要差别
package com.zhong.struct;
public class whileDemo01{
public static void main(String [] args){
//1+2+3+4+......+100=?
int i = 0;
sum = 0;
do{
sum = sum + i;
i++;
}while(i<=100)
System.out.printIn(sum);
}
}
先输出后自增
package com.zhong.struct;
public class whileDemo01{
public static void main(String [] args){
int a = 0;
while(a<0){
System.out.printIn(a);
a++
}
System.out.printIn(===============);
do{
System.out.printIn(a);
a++
}while(a<0);
}
}