<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # C++ `while`和`do...while`循環 > 原文: [https://www.programiz.com/cpp-programming/do-while-loop](https://www.programiz.com/cpp-programming/do-while-loop) #### 在本教程中,我們將借助一些示例來學習 C++ 編程中`while`和`do...while`循環的用法。 在計算機編程中,循環用于重復代碼塊。 例如,假設我們要顯示一條消息 100 次。 然后,我們可以使用循環來代替寫`print`語句 100 次。 那只是一個簡單的例子; 通過有效利用循環,我們可以在程序中實現更高的效率和復雜性。 C++ 中有 **3** 種循環。 1. `for`循環 2. `while`循環 3. `do...while`循環 在上一教程中,我們了解了 [C++ `for`循環](/cpp-programming/for-loop)。 在這里,我們將學習`while`和`do...while`循環。 * * * ## C++ `while`循環 `while`循環的語法為: ```cpp while (condition) { // body of the loop } ``` 這里, * `while`循環求值`condition` * 如果`condition`求值為`true`,則將執行`while`循環內的代碼。 * 再次求值`condition`。 * 該過程一直持續到`condition`為`false`為止。 * 當`condition`求值為`false`時,循環終止。 要了解有關`conditions`的更多信息,請訪問 [C++ 關系和邏輯運算符](/cpp-programming/relational-logical-operators)。 * * * ### `While`循環流程圖 ![C++ while loop flowchart](https://img.kancloud.cn/f3/d9/f3d9c3f30adbf3e28b8537d1eb06d483.png "C++ while loop flowchart") C++ `while`循環流程圖 * * * ### 示例 1:顯示從 1 到 5 的數字 ```cpp // C++ Program to print numbers from 1 to 5 #include <iostream> using namespace std; int main() { int i = 1; // while loop from 1 to 5 while (i <= 5) { cout << i << " "; ++i; } return 0; } ``` **輸出** ```cpp 1 2 3 4 5 ``` 該程序的工作原理如下。 | 迭代 | 變量 | `i <= 5` | 行為 | | --- | --- | --- | --- | | 1 | `i = 1` | `true` | 打印 1,將`i`增加到`2`。 | | 2 | `i = 2` | `true` | 打印 2,將`i`增大為`3`。 | | 3 | `i = 3` | `true` | 打印 3,將`i`增大為`4` | | 4 | `i = 4` | `true` | 打印 4,將`i`增加到`5`。 | | 5 | `i = 5` | `true` | 打印 5,將`i`增加到`6`。 | | 6 | `i = 6` | `false` | 循環終止 | * * * ### 示例 2:僅正數之和 ```cpp // program to find the sum of positive numbers // if the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include <iostream> using namespace std; int main() { int number; int sum = 0; // take input from the user cout << "Enter a number: "; cin >> number; while (number >= 0) { // add all positive numbers sum += number; // take input again if the number is positive cout << "Enter a number: "; cin >> number; } // display the sum cout << "\nThe sum is " << sum << endl; return 0; } ``` **輸出** ```cpp Enter a number: 6 Enter a number: 12 Enter a number: 7 Enter a number: 0 Enter a number: -2 The sum is 25 ``` 在此程序中,提示用戶輸入一個數字,該數字存儲在變量`num`中。 為了存儲數字的總和,我們聲明一個變量`sum`并將其初始化為`0`的值。 `while`循環繼續,直到用戶輸入一個負數。 在每次迭代期間,用戶輸入的數字將添加到`sum`變量中。 當用戶輸入負數時,循環終止。 最后,顯示總和。 * * * ## C++ `do...while`循環 `do...while`循環是`while`循環的一種變體,具有一個重要的區別:`do...while`循環的主體在檢查`condition`之前執行一次。 其語法為: ```cpp do { // body of loop; } while (condition); ``` Here, * 循環的主體首先執行。 然后求值`condition`。 * 如果`condition`求值為`true`,則將再次執行`do`語句內的循環主體。 * 再次求值`condition`。 * 如果`condition`求值為`true`,則將再次執行`do`語句內的循環主體。 * 該過程一直持續到`condition`求值為`false`為止。 然后循環停止。 * * * ### `do...while`循環流程圖 ![C++ do...while loop flowchart](https://img.kancloud.cn/f3/d9/f3d9c3f30adbf3e28b8537d1eb06d483.png "C++ do...while loop flowchart") C++ `do...while`循環流程圖 * * * ### 示例 3:顯示從 1 到 5 的數字 ```cpp // C++ Program to print numbers from 1 to 5 #include <iostream> using namespace std; int main() { int i = 1; // do...while loop from 1 to 5 do { cout << i << " "; ++i; } while (i <= 5); return 0; } ``` **輸出** ```cpp 1 2 3 4 5 ``` Here is how the program works. | 迭代 | 變量 | `i <= 5` | 行為 | | --- | --- | --- | --- | | ? | `i = 1` | 未檢查 | 打印 1,將`i`增加到 2 | | 1 | `i = 2` | `true` | 打印 2 ,將`i`增加到 3 | | 2 | `i = 3` | `true` | 打印 3 ,將`i`增加到 4 | | 3 | `i = 4` | `true` | 打印 4 ,將`i`增加到 5 | | 4 | `i = 5` | `true` | 打印 5 ,將`i`增大為 **6** | | 5 | `i = 6` | `false` | 循環結束 | * * * ### 示例 4:僅正數之和 ```cpp // program to find the sum of positive numbers // If the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include <iostream> using namespace std; int main() { int number = 0; int sum = 0; do { sum += number; // take input from the user cout << "Enter a number: "; cin >> number; } while (number >= 0); // display the sum cout << "\nThe sum is " << sum << endl; return 0; } ``` **輸出 1** ```cpp Enter a number: 6 Enter a number: 12 Enter a number: 7 Enter a number: 0 Enter a number: -2 The sum is 25 ``` 在此,`do...while`循環繼續進行,直到用戶輸入一個負數。 當數字為負數時,循環終止;否則為 0。 負數不會添加到`sum`變量中。 **輸出 2** ```cpp Enter a number: -6 The sum is 0. ``` 如果用戶輸入一個負數,則`do...while`循環的主體僅運行一次。 * * * ## 無限`while`循環 如果循環的`condition`始終為`true`,則循環運行無限次(直到內存已滿)。 例如, ```cpp // infinite while loop while(true) { // body of the loop } ``` 這是無限`do...while`循環的示例。 ```cpp // infinite do...while loop int count = 1; do { // body of loop } while(count == 1); ``` 在上述程序中,`condition`始終為`true`。 因此,循環體將運行無限次。 * * * ## `for` vs `while`循環 當已知迭代次數時,通常使用`for`循環。 例如, ```cpp // This loop is iterated 5 times for (int i = 1; i <=5; ++i) { // body of the loop } ``` 在這里,我們知道`for`循環將執行 5 次。 但是,`while`和`do...while`循環通常在迭代次數未知的情況下使用。 例如, ```cpp while (condition) { // body of the loop } ``` * * * 查看以下示例以了解更多信息: * [C++ 程序:顯示斐波那契數列](/cpp-programming/examples/fibonacci-series) * [C++ 程序:查找 GCD](https://www.geeksforgeeks.org/c-program-find-gcd-hcf-two-numbers/) * [C++ 程序:查找 LCM](/cpp-programming/examples/lcm)
                  <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>

                              哎呀哎呀视频在线观看