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