<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之旅 廣告
                # C++ 中的`break`語句 > 原文: [https://beginnersbook.com/2017/08/cpp-break-statement/](https://beginnersbook.com/2017/08/cpp-break-statement/) **`break`語句**用于以下兩種情況: a)使用`break`語句立即退出循環。每當在循環內遇到`break`語句時,控制流就會直接從循環中退出。它與`if`語句一起使用,只能在循環內部使用(參見下面的示例),以便它僅在特定條件下發生。 b)用于`switch-case`控制結構。通常,在`switch case`中的所有情況都跟一個`break`語句,以避免后續的情況(參見下面的例子)執行。無論何時在`switch-case`塊中遇到,控制流都從`switch-case`體中出來。 #### `break`語句的語法 ```cpp break; ``` ## `break`語句流程圖 ![C++ break statement](https://img.kancloud.cn/13/c6/13c6e4314a645177ecb487d48d7c7b0a_450x400.jpg) ## 示例 - 在`while`循環中使用`break`語句 在下面的示例中,我們有一個從 10 到 200 運行的`while`循環,但由于我們有一個在循環計數器,變量值達到 12 時遇到`break`語句,循環終止并且控制流跳轉到程序中循環體之后的下一個語句。 ```cpp #include <iostream> using namespace std; int main(){ int num =10; while(num<=200) { cout<<"Value of num is: "<<num<<endl; if (num==12) { break; } num++; } cout<<"Hey, I'm out of the loop"; return 0; } ``` **輸出:** ```cpp Value of num is: 10 Value of num is: 11 Value of num is: 12 Hey, I'm out of the loop ``` ## 示例:`for`循環中的`break`語句 ```cpp #include <iostream> using namespace std; int main(){ int var; for (var =200; var>=10; var --) { cout<<"var: "<<var<<endl; if (var==197) { break; } } cout<<"Hey, I'm out of the loop"; return 0; } ``` **輸出:** ```cpp var: 200 var: 199 var: 198 var: 197 Hey, I'm out of the loop ``` ## 示例:`switch-case`中的`break`語句 ```cpp #include <iostream> using namespace std; int main(){ int num=2; switch (num) { case 1: cout<<"Case 1 "<<endl; break; case 2: cout<<"Case 2 "<<endl; break; case 3: cout<<"Case 3 "<<endl; break; default: cout<<"Default "<<endl; } cout<<"Hey, I'm out of the switch case"; return 0; } ``` **輸出:** ```cpp Case 2 Hey, I'm out of the switch case ``` 在這個例子中,我們在每個`case`塊之后都有`break`語句,這是因為如果我們沒有它,那么后續的`case`塊也會執行。沒有`break`的同一程序的輸出將是: ```cpp Case 2 Case 3 Default Hey, I'm out of the switch case ```
                  <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>

                              哎呀哎呀视频在线观看