The while statement is used for repeated execution as long as an expression is true:
while用于控制重复执行, 只要满足条件表达式为真:
while_stmt |
::= | "while" expression ":" suite |
["else" ":" suite] |
This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.
while会重复地计算表达式的值, 并且如果为真,就执行第一个语句序列; 如果为假(可能在第一次比较时),就执行else子句(如果给出), 并退出循环.
A break statement executed in the first suite terminates the loop without executing the else clause's suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.
在第一个语句序列中的break语句可以实现不执行else子句而退出循环.在第一个语句序列中的continue语句可以跳过该子句的其余部分, 直接进行下次的表达式的测试.