Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 1.33 KB

5.14.while.md

File metadata and controls

33 lines (23 loc) · 1.33 KB

5.14.while

一个普通的 while 循环由 while 关键字开始,后跟一个开口的圆括号 (,其后是条件表达式和一个闭口的圆括号 ) 。然后跟一个循环体表达式:

A normal while loop starts with the while keyword, followed by an opening parenthesis (, the condition expression and a closing paranthesis ). After that follows the loop body expression:

while(condition) expression; 

条件表达式必须为 Bool 类型。

The condition expression has to be of type Bool.

对应每次迭代,条件表达式都会执行。如果它执行结果为 false ,循环则停止,否则执行循环体。

Upon each iteration, the condition expression is evaluated. If it evaluates to false, the loop stops, otherwise it evaluates the loop body expression.

class Main {
    static public function main() { 
        var f = 0.0; 
        while (f < 0.5) { 
            trace(f); 
            f = Math.random(); 
        } 
    }
} 

这类的while循环不保证一定执行循环体表达式:如果条件从开始就不匹配,它不会执行。这和 do-while循环(第5.15节)不同。

This kind of while-loop is not guaranteed to evaluate the loop body expression at all: If the condition does not hold from the start, it is never evaluated. This is different for do-while loops (5.15).