本文概要
Java的do-while循环用于迭代程序数次的一部分。如果迭代的次数是不固定的,你必须有至少执行一次循环,建议使用do-while循环。
在Java做的,在执行循环至少一次,因为条件的循环体后检查。
句法:
do{
//code to be executed
}while(condition);
例:
public class DoWhileExample {
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}
输出:
1
2
3
4
5
6
7
8
9
10
Java的不定式do-while循环
如果你在通过真do-while循环,这将是不定式do-while循环。
句法:
do{
//code to be executed
}while(true);
例:
public class DoWhileExample2 {
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true);
}
}
输出:
infinitive do while loop
infinitive do while loop
infinitive do while loop
ctrl+c
现在,你需要按CTRL + C来退出程序。
评论前必须登录!
注册