python 中 while循环的语法如下
1 |
"while" <a class="reference internal" href="https://docs.python.org/3/reference/expressions.html#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> ":" <a class="reference internal" href="https://docs.python.org/3/reference/compound_stmts.html#grammar-token-suite"><code class="xref docutils literal notranslate"><span class="pre">suite</span></code></a> |
1 |
["else" ":" <a class="reference internal" href="https://docs.python.org/3/reference/compound_stmts.html#grammar-token-suite"><code class="xref docutils literal notranslate"><span class="pre">suite</span></code></a>] |
其中只要 expression
的结果为True,就一直执行suite中的语句。
如果要中断执行,可以添加break语句。
如果要跳过suite中的某些语句,可以添加continue语句。
代码如下:
i=9
while i>3:
i=i-1
print(i)
#continue语句的作用是跳出这里的执行语句,直接开始下一个循环
continue
#break语句的作用是只执行一次循环就退出整个循环
break
else:
print(‘初始值就是小于10的’)
发表评论