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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 中的`for`循環 > 原文: [https://beginnersbook.com/2017/08/cpp-for-loop/](https://beginnersbook.com/2017/08/cpp-for-loop/) 循環用于重復執行語句塊,直到滿足特定條件。例如,當您顯示從 1 到 100 的數字時,您可能希望將變量的值設置為 1 并將其顯示 100 次,在每次循環迭代時將其值增加 1。 在 C++ 中,我們有三種類型的基本循環:`for`,[`while`](https://beginnersbook.com/2017/08/cpp-while-loop/)和[`do-while`](https://beginnersbook.com/2017/08/cpp-do-while-loop/) 。在本教程中,我們將學習如何在 C++ 中使用`for` 循環。 #### `for`循環的語法 ```cpp for(initialization; condition ; increment/decrement) { C++ statement(s); } ``` ## `for`循環的執行流程 當程序執行時,解釋器總是跟蹤將要執行的語句。我們將其稱為控制流程或程序的執行流程。 ![C++ for loop flow diagram](https://img.kancloud.cn/3e/65/3e657bb9c43403711b5bff436dad0453_400x400.jpg) **第一步:**在`for`循環中,初始化只發生一次,這意味著`for`循環的初始化部分只執行一次。 **第二步:** `for`循環中的條件在每次循環迭代時進行計算,如果條件為真,則`for`循環體內的語句將被執行。一旦條件返回`false`,`for`循環中的語句就不會執行,并且控制被轉移到程序中`for`循環后的下一個語句。 **第三步:**每次執行`for`循環體后,`for`循環的遞增/遞減部分更新循環計數器。 **第四步:**第三步后,控制跳轉到第二步,重新求值條件。 > 從第二到第四的步驟重復,直到循環條件返回`false`。 ## C++ 中的簡單`for`循環示例 這里,在循環初始化部分中,將變量`i`的值設置為 1,條件是`i <= 6`,并且在每次循環迭代中,`i`的值遞增 1。 ```cpp #include <iostream> using namespace std; int main(){ for(int i=1; i<=6; i++){ /* This statement would be executed * repeatedly until the condition * i<=6 returns false. */ cout<<"Value of variable i is: "<<i<<endl; } return 0; } ``` **輸出:** ```cpp Value of variable i is: 1 Value of variable i is: 2 Value of variable i is: 3 Value of variable i is: 4 Value of variable i is: 5 Value of variable i is: 6 ``` ## C++ 中的無限循環 當循環重復執行并且永不停止時,循環被認為是無限的。這通常是錯誤的。當你在`for`循環中設置條件時它永遠不會返回`false`,它就會變成無限循環。 **例如:** ```cpp #include <iostream> using namespace std; int main(){ for(int i=1; i>=1; i++){ cout<<"Value of variable i is: "<<i<<endl; } return 0; } ``` 這是一個無限循環,因為我們遞增`i`的值,因此它總是滿足條件`i <= 1`,條件永遠不會返回`false`。 這是無限`for`循環的另一個例子: ```cpp // infinite loop for ( ; ; ) { // statement(s) } ``` ## 示例:使用`for`循環顯示數組元素 ```cpp #include <iostream> using namespace std; int main(){ int arr[]={21,9,56,99, 202}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ for(int i=0; i<5; i++){ cout<<arr[i]<<endl; } return 0; } ``` **輸出:** ```cpp 21 9 56 99 202 ```
                  <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>

                              哎呀哎呀视频在线观看