<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++ `for`循環 > 原文: [https://www.programiz.com/cpp-programming/for-loop](https://www.programiz.com/cpp-programming/for-loop) #### 在本教程中,我們將在一些示例的幫助下了解 C++ `for`循環及其工作原理。 在計算機編程中,循環用于重復代碼塊。 例如,假設我們要顯示一條消息 100 次。 然后,我們可以使用循環來代替寫`print`語句 100 次。 那只是一個簡單的例子; 通過有效地使用循環,我們可以在程序中實現更高的效率和復雜性。 C++ 中有 3 種循環類型。 * `for`循環 * `while`循環 * `do...while`循環 本教程重點介紹 C++ `for`循環。 我們將在以后的教程中學習其他類型的循環。 * * * ## C++ `for`循環 `for`循環的語法為: ```cpp for (initialization; condition; update) { // body of-loop } ``` 這里, * `initialization` - 初始化變量,僅執行一次 * `condition` - 如果執行`true`,則執行`for`循環的主體 如果執行`false`,則終止`for`循環 * `update` - 更新初始化變量的值,然后再次檢查條件 要了解有關`conditions`的更多信息,請查看我們的 [C++ 關系和邏輯運算符](/cpp-programming/relational-logical-operators)教程。 * * * ## C++ 中`for`循環的流程圖 ![C++ for loop flowchart](https://img.kancloud.cn/f3/d9/f3d9c3f30adbf3e28b8537d1eb06d483.png "C++ for loop flowchart") C++ 中`for`循環的流程圖 * * * ### 示例 1:從 1 到 5 打印數字 ```cpp #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << 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:顯示文本 5 次 ```cpp // C++ Program to display a text 5 times #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << "Hello World! " << endl; } return 0; } ``` **輸出** ```cpp Hello World! Hello World! Hello World! Hello World! Hello World! ``` 該程序的工作原理如下 | 迭代 | 變量 | `i <= 5` | 行為 | | --- | --- | --- | --- | | 1 | `i = 1` | `true` | 打印`Hello World!`并將`i`增加到`2`。 | | 2 | `i = 2` | `true` | 打印`Hello World!`并將`i`增加到`3`。 | | 3 | `i = 3` | `true` | 打印`Hello World!`并將`i`增加到`4`。 | | 4 | `i = 4` | `true` | 打印`Hello World!`并將`i`增加到`5`。 | | 5 | `i = 5` | `true` | 打印`Hello World!`并將`i`增加到`6`。 | | 6 | `i = 6` | `false` | 循環終止 | * * * ### 示例 3:查找前 n 個自然數的總和 ```cpp // C++ program to find the sum of first n natural numbers // positive integers such as 1,2,3,...n are known as natural numbers #include <iostream> using namespace std; int main() { int num, sum; sum = 0; cout << "Enter a positive integer: "; cin >> num; for (int count = 1; count <= num; ++count) { sum += count; } cout << "Sum = " << sum << endl; return 0; } ``` **輸出** ```cpp Enter a positive integer: 10 Sum = 55 ``` 在上面的示例中,我們有兩個變量`num`和`sum`。`sum`變量分配有`0`,`num`變量分配有用戶提供的值。 請注意,我們使用了`for`循環。 ```cpp for(int count = 1; count <= num; ++count) ``` Here, * `int count = 1`:初始化`count`變量 * `count <= num`:只要`count`小于或等于`num`,就運行循環 * `++count`:每次迭代將`count`變量增加 1 當`count`變為`11`時,`condition`為`false`,并且`sum`等于`0 + 1 + 2 + ... + 10`。 * * * ## 基于范圍的`for`循環 在 C++ 11 中,引入了一個新的基于范圍的`for`循環來處理諸如**數組**和**向量**之類的集合。 其語法為: ```cpp for (variable : collection) { // body of loop } ``` 在此,對于`collection`中的每個值,都會執行`for`循環,并將該值分配給`var`。 * * * ### 示例 4:基于范圍的循環 ```cpp #include <iostream> using namespace std; int main() { int num_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int n : num_array) { cout << n << " "; } return 0; } ``` **輸出** ```cpp 1 2 3 4 5 6 7 8 9 10 ``` 在上面的程序中,我們聲明并初始化了一個名為`num_array`的`int`數組。 它有 10 個項目。 在這里,我們使用了基于范圍的`for`循環來訪問數組中的所有項目。 * * * ### C++ 無限循環 如果`for`循環中的`condition`始終為`true`,則它將永遠運行(直到內存已滿)。 例如, ```cpp // infinite for loop for(int i = 1; i > 0; i++) { // block of code } ``` 在上面的程序中,`condition`始終為`true`,它將無限次運行代碼。 * * * 查看以下示例以了解更多信息: * [C++ 程序:計算自然數的總和](/cpp-programming/examples/sum-natural-number) * [C++ 程序:查找階乘](/cpp-programming/examples/factorial) * [C++ 程序:生成乘法表](/cpp-programming/examples/multiplication-table) * * * 在下一個教程中,我們將學習`while`和`do...while`循環。
                  <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>

                              哎呀哎呀视频在线观看