The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The 当特定条件为真时,while
statement continually executes a block of statements while a particular condition is true
.while
语句持续执行语句块。Its syntax can be expressed as:其语法可以表示为:
while (expression) { statement(s) }
The while
statement evaluates expression, which must return a boolean
value.while
语句计算表达式,该表达式必须返回boolean
值。If the expression evaluates to 如果表达式的计算结果为true
, the while
statement executes the statement(s) in the while
block.true
,则while
语句将执行while
块中的语句。The while
statement continues testing the expression and executing its block until the expression evaluates to false
.while
语句继续测试表达式并执行其块,直到表达式的计算结果为false
。Using the 使用while
statement to print the values from 1 through 10 can be accomplished as in the following WhileDemo
program:while
语句打印1到10之间的值可以通过以下WhileDemo
程序完成:
class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }
You can implement an infinite loop using the 可以使用while
statement as follows:while
语句实现无限循环,如下所示:
while (true){ // your code goes here }
The Java programming language also provides a Java编程语言还提供do-while
statement, which can be expressed as follows:do-while
语句,该语句可以表示为:
do { statement(s) } while (expression);
The difference between do-while
and while
is that do-while
evaluates its expression at the bottom of the loop instead of the top.do-while
和while
之间的区别在于do-while
在循环的底部而不是顶部计算其表达式。Therefore, the statements within the 因此,do
block are always executed at least once, as shown in the following DoWhileDemo
program:do
块内的语句始终至少执行一次,如以下DoWhileDemo
程序所示:
class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }