/*
do...while循环的基本格式:
扩展格式;
初始化语句;
do {
循环体语句;
控制条件语句;
}while(判断条件语句);
循环语句的区别:
do...while循环至少执行一次循环体。
而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
那么,我们一般使用哪种循环呢?
优先考虑for,其次考虑while,最后考虑do...while
注意死循环:
A:一定要注意控制条件语句控制的那个变量的问题,不要弄丢了,否则就容易死循环。
B:两种最简单的死循环格式
while(true){...}
for(;;){...}
*/
class DoWhileDemo {
public static void main(String[] args) {
//输出10次HelloWorld。
int x = 0;
do {
System.out.println("HelloWorld");
x++;
}while(x<10);
System.out.println("--------------");
//求和1-100
int sum = 0;
int a = 1;
do {
sum += a;
a++;
}while(a<=100);
System.out.println(sum);
}
}
class DoWhileDemo2 {
public static void main(String[] args) {
int x = 3;
while(x < 3) {
System.out.println("我爱林青霞");
x++;
}
System.out.println("--------------");
int y = 3;
do {
System.out.println("我爱林青霞");
y++;
}while(y < 3);
}
}
class DoWhileDemo3 {
public static void main(String[] args) {
int x = 0;
while(x < 10) {
System.out.println(x);
x++;
}
System.out.println("--------------");
/*
while(true) {
System.out.println("今天我很高兴,学习了死循环");
}
*/
for(;;){
System.out.println("今天我很高兴,学习了死循环");
}
//System.out.println("--------------");
}
}