do...while循环
- 对于while语句而言,如果不满足条件,则不能进入循环,有时即使不满足条件,也至少进入一次。
- do...while循环和while循环相似,不同的是,do...while循环至少执行一次。
do {
//代码语句
}while(布尔表达式);
- While和do...while的区别:
- while先判断后执行。do...while先执行后判断。
- 差别:do...while总是保证循环体被至少执行一次!
package com.earl.struct;
public class DoWhile01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);//输出5050
}
}
package com.earl.struct;
public class DoWhile02 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("==============");
do {
System.out.println(a);
a++;
}while (a<0);
}
}
结果:
标签:do,...,int,while,循环,public From: https://www.cnblogs.com/blogearl/p/17052710.html