<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://www.programiz.com/cpp-programming/break-continue](https://www.programiz.com/cpp-programming/break-continue) #### 在本教程中,我們將在示例的幫助下了解`break`語句及其在循環中的工作。 在計算機編程中,`break`語句用于終止使用它的循環。 `break`語句的語法為: ```cpp break; ``` 在學習`break`語句之前,請確保您了解: * [C++ `for`循環](/cpp-programming/for-loop "C++ for loop") * [C++ `if...else`](/cpp-programming/for-loop "C++ if...else") * [C++ `while`循環](/cpp-programming/do-while-loop "C++ while loop") * * * ## C++ `break`語句的原理 ![Working of C++ break Statement](https://img.kancloud.cn/24/21/2421af23e2afb531be54dea59c41f0f4_690x824.png "Working of break statement in C++") C++ 中的`break`語句原理 * * * ## 示例 1:`for`循環和`break` ```cpp // program to print the value of i #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { // break condition if (i == 3) { break; } cout << i << endl; } return 0; } ``` **輸出** ```cpp 1 2 ``` 在上述程序中,`for`循環用于在每次迭代中打印`i`的值。 在這里,請注意代碼: ```cpp if (i == 3) { break; } ``` 這意味著,當`i`等于 **3** 時,`break`語句終止循環。 因此,輸出不包含大于或等于 3 的值。 注意:`break`語句通常與決策語句一起使用。 * * * ## 示例 2:`while`循環與`break` ```cpp // program to find the sum of positive numbers // if the user enters a negative numbers, break ends the loop // the negative number entered is not added to sum #include <iostream> using namespace std; int main() { int number; int sum = 0; while (true) { // take input from the user cout << "Enter a number: "; cin >> number; // break condition if (number < 0) { break; } // add all positive numbers sum += number; } // display the sum cout << "The sum is " << sum << endl; return 0; } ``` **輸出** ```cpp Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: -5 The sum is 6\. ``` 在上述程序中,用戶輸入一個數字。`while`循環用于打印用戶輸入的數字的總和。 在這里,請注意代碼, ```cpp if(number < 0) { break; } ``` 這意味著,當用戶輸入一個負數時,`break`語句終止循環,并執行循環外的代碼。 `while`循環繼續,直到用戶輸入一個負數。 * * * ## 打破嵌套循環 當`break`與嵌套循環一起使用時,`break`終止內部循環。 例如, ```cpp // using break statement inside // nested for loop #include <iostream> using namespace std; int main() { int number; int sum = 0; // nested for loops // first loop for (int i = 1; i <= 3; i++) { // second loop for (int j = 1; j <= 3; j++) { if (i == 2) { break; } cout << "i = " << i << ", j = " << j << endl; } } return 0; } ``` **輸出** ```cpp i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 ``` 在上述程序中,當`i == 2`時執行`break`語句。 它終止了內部循環,程序的控制流移至外部循環。 因此,`i = 2`的值永遠不會顯示在輸出中。 * * * `break`語句也與`switch`語句一起使用。 要了解更多信息,請訪問 [C++ `switch`語句](/cpp-programming/switch-case "C++ switch statement")。
                  <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>

                              哎呀哎呀视频在线观看