<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python `break`和`continue` > 原文: [https://www.programiz.com/python-programming/break-continue](https://www.programiz.com/python-programming/break-continue) #### 在本文中,您將學習使用`break`和`continue`語句來更改循環流。 ## Python 中 `break`和`continue`的用途是什么? 在 Python 中,`break`和`continue`語句可以更改常規循環的流程。 循環遍歷代碼塊,直到測試表達式為假,但有時我們希望在不檢查測試表達式的情況下終止當前迭代甚至整個循環。 在這些情況下,使用`break`和`continue`語句。 * * * ## Python `break`語句 `break`語句終止包含它的循環。 程序的控制權在循環體之后立即傳遞到該語句。 如果`break`語句位于嵌套循環內(另一個循環內的循環),則`break`語句將終止最里面的循環。 ### `break`語法 ```py break ``` ### `break`流程圖 ![Flowchart of break statement in Python](https://img.kancloud.cn/9b/75/9b75979996a9050923d9a16275c762c4_500x585.png "break Statement") Python 中`break`語句的流程圖 下面顯示了`for`循環中和`while`循環中[`break`語句](https://www.programiz.com/python-programming/for-loop)的工作。 ![How the break statement works in Python](https://img.kancloud.cn/a9/c7/a9c734e52357181d51e4dac4c5f5db07_320x349.png "How break statement works") `break`語句的原理 ### 示例:Python `break` ```py # Use of break statement inside the loop for val in "string": if val == "i": break print(val) print("The end") ``` **輸出** ```py s t r The end ``` 在此程序中,我們循環訪問`"string"`序列。 我們檢查字母是否為`i`,由此我們從循環中斷開。 因此,我們在輸出中看到直到`i`的所有字母都被打印。 之后,循環終止。 * * * ## Python `continue`語句 `continue`語句僅用于當前迭代,用于跳過循環內的其余代碼。 循環不會終止,但會繼續進行下一個迭代。 ### `continue`語法 ```py continue ``` ### `continue`流程圖 ![Flowchart of continue statement in Python](https://img.kancloud.cn/d1/ec/d1ec06ad18ae70fabc5c5c2bca4efde8_400x468.png "continue Statement") Python 中`continue`語句的流程圖 `for`和`while`循環中的`continue`語句的工作如下所示。 ![How continue statement works in python](https://img.kancloud.cn/be/e2/bee211ebf0bd7f5f120437d47cdcb221_320x321.png "How continue statement works") `continue`語句如何在 python 中工作 ### 示例:Python `continue` ```py # Program to show the use of continue statement inside loops for val in "string": if val == "i": continue print(val) print("The end") ``` **輸出**: ```py s t r n g The end ``` 該程序與上面的示例相同,除了`break`語句已替換為`continue`。 如果字符為`i`,我們將繼續執行循環,而不執行該塊的其余部分。 因此,我們在輸出中看到,除了`i`以外的所有字母都已打印。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看