while
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<=99){
System.out.println(i++);
}
}
}
while (true){
//表达式
}
- 只要条件为true 就是死循环
- do while
public class DoWhileDemo01 {
public static void main(String[] args) {
//1+2+3+4+5+6+7+8+9....100
int i = 0;
int sum = 0;
do {
sum = sum+i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
-
两者区别
public class DoWhileDemo02 { 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++); } while(a<0); } }