<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`循環 > 原文: [https://beginnersbook.com/2017/08/cpp-while-loop/](https://beginnersbook.com/2017/08/cpp-while-loop/) 在上一篇教程中,我們討論了[`for`循環](https://beginnersbook.com/2017/08/cpp-for-loop/) 。在本教程中,我們將討論`while`循環。如前所述,循環用于重復執行程序語句塊,直到給定的循環條件返回`false`。 #### `while`循環的語法 ```cpp while(condition) { statement(s); } ``` ## 循環如何工作? 在`while`循環中,首先計算條件,如果它返回`true`,則執行`while`循環中的語句,這會重復發生,直到條件返回`false`。當條件返回`false`時,控制流退出循環并跳轉到程序中的`while`循環后的下一個語句。 **注意:**使用`while`循環時要注意的重點是,我們需要在`while`循環中使用遞增或遞減語句,以便循環變量在每次迭代時都會發生變化,并且在某些情況下返回`false`。這樣我們就可以結束`while`循環的執行,否則循環將無限期地執行。 ### `while`循環流程圖 ![c++ while loop flow diagram](https://img.kancloud.cn/a7/8a/a78a67b0e3982e46a8fa26139d139a2f_400x400.jpg) ## C++中的`while`循環示例 ```cpp #include <iostream> using namespace std; int main(){ int i=1; /* The loop would continue to print * the value of i until the given condition * i<=6 returns false. */ while(i<=6){ cout<<"Value of variable i is: "<<i<<endl; i++; } } ``` **輸出:** ```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 ``` ## 無限循環 永遠不停止的`while`循環被認為是無限循環,當我們以這樣的方式給出條件,以使它永遠不會返回`false`時,循環變為無限并且無限地重復。 **無限循環的一個例子:** 這個循環永遠不會結束,因為我從 1 開始遞減`i`的值,因此條件`i <= 6`永遠不會返回`false`。 ```cpp #include <iostream> using namespace std; int main(){ int i=1; while(i<=6) { cout<<"Value of variable i is: "<<i<<endl; i--; } } ``` ## 示例:使用`while`循環顯示數組元素 ```cpp #include <iostream> using namespace std; int main(){ int arr[]={21,87,15,99, -12}; /* The array index starts with 0, the * first element of array has 0 index * and represented as arr[0] */ int i=0; while(i<5){ cout<<arr[i]<<endl; i++; } } ``` **輸出:** ```cpp 21 87 15 99 -12 ```
                  <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>

                              哎呀哎呀视频在线观看