<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 `while`和`do...while`循環 > 原文: [https://www.programiz.com/c-programming/c-do-while-loops](https://www.programiz.com/c-programming/c-do-while-loops) #### 在本教程中,您將在示例的幫助下學習在 C 編程中創建`while`和`do...while`循環。 在編程中,循環用于重復代碼塊,直到滿足指定條件為止。 C 編程具有三種類型的循環。 1. `for`循環 2. `while`循環 3. `do...while`循環 在上一個教程中,我們了解了`for`循環。 在本教程中,我們將學習`while`和`do..while`循環。 * * * ## `while`循環 `while`循環的語法為: ```c while (testExpression) { // statements inside the body of the loop } ``` * * * ### `while`循環如何工作? * `while`循環求值括號`()`內的測試表達式。 * 如果測試表達式為`true`,則執行`while`循環體內的語句。 然后,再次求值測試表達式。 * 該過程一直進行到測試表達式被求值為`false`為止。 * 如果測試表達式為假,則循環終止(結束)。 要了解有關測試表達式的更多信息(將測試表達式求值為真和假時),請查看[關系](/c-programming/c-operators#relational "C Relational operators")和[邏輯運算符](/c-programming/c-operators#logical "C logical operators")。 * * * ### `While`循環流程圖 ![flowchart of while loop in C programming](https://img.kancloud.cn/ee/67/ee67273345891d47efcaab4a8583d432_320x326.png "while loop flowchart") * * * ### 示例 1:`while`循環 ```c // Print numbers from 1 to 5 #include <stdio.h> int main() { int i = 1; while (i <= 5) { printf("%d\n", i); ++i; } return 0; } ``` **輸出** ```c 1 2 3 4 5 ``` 在這里,我們已經將`i`初始化為 1。 1. 當`i`為 1 時,測試表達式`i <= 5`為`true`。 因此,執行`while`循環的主體。 在屏幕上打印 1,并且`i`的值增加到 2。 2. 現在,`i`為 2,則測試表達式`i <= 5`再次為真。 再次執行`while`循環的主體。 在屏幕上打印 2,`i`的值增加到 3。 3. 該過程一直進行到`i`變為 6 為止。當`i`為 6 時,測試表達式`i <= 5`為假,循環終止。 * * * ## `do...while`循環 `do..while`循環類似于`while`循環,但有一個重要區別。`do...while`循環的主體至少執行一次。 只有這樣,才對測試表達式求值。 `do...while`循環的語法為: ```c do { // statements inside the body of the loop } while (testExpression); ``` * * * ### `do...while`循環如何工作? * `do...while`循環的主體執行一次。 只有這樣,才對測試表達式求值。 * 如果測試表達式為`true`,則再次執行循環主體并求值測試表達式。 * 這個過程一直進行到測試表達式變為假。 * 如果測試表達式為假,則循環結束。 * * * ### `do...while`循環流程圖 ![do while loop flowchart in C programming](https://img.kancloud.cn/5b/db/5bdb352c0a8365bb43f5847eae6d5ba5_260x272.png "do while loop flowchart") * * * ### 示例 2:`do...while`循環 ```c // Program to add numbers until the user enters zero #include <stdio.h> int main() { double number, sum = 0; // the body of the loop is executed at least once do { printf("Enter a number: "); scanf("%lf", &number); sum += number; } while(number != 0.0); printf("Sum = %.2lf",sum); return 0; } ``` **輸出** ```c Enter a number: 1.5 Enter a number: 2.4 Enter a number: -3.4 Enter a number: 4.2 Enter a number: 0 Sum = 4.70 ```
                  <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>

                              哎呀哎呀视频在线观看