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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 中的`while`循環 > 原文: [https://beginnersbook.com/2015/03/while-loop-in-java-with-examples/](https://beginnersbook.com/2015/03/while-loop-in-java-with-examples/) 在上一個教程中,我們討論了[`for`循環](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/) 。在本教程中,我們將討論`while`循環。如前一個教程中所討論的,循環用于重復執行一組語句,直到滿足特定條件。 #### `while`循環的語法 ```java while(condition) { statement(s); } ``` ## `while`循環如何工作? 在`while`循環中,首先計算條件,如果它返回`true`,則`while`循環中的語句執行。當條件返回`false`時,控制流退出循環并跳轉到`while`循環后的下一個語句。 > 注意:使用`while`循環時要注意的重點是我們需要在`while`循環中使用遞增或遞減語句,以便循環變量在每次迭代時都會更改,并且在某些情況下返回`false`。這樣我們就可以結束`while`循環的執行,否則循環將無限期地執行。 ![while loop java](https://img.kancloud.cn/a7/8a/a78a67b0e3982e46a8fa26139d139a2f_400x400.jpg) ## 簡單的`while`循環示例 ```java class WhileLoopExample { public static void main(String args[]){ int i=10; while(i>1){ System.out.println(i); i--; } } } ``` **輸出:** ```java 10 9 8 7 6 5 4 3 2 ``` ## 無限循環 ```java class WhileLoopExample2 { public static void main(String args[]){ int i=10; while(i>1) { System.out.println(i); i++; } } } ``` 這個循環永遠不會結束,它是一個無限循環。這是因為條件是`i > 1`,因為我們在循環中遞增`i`的值,所以它總是為真。 這是無限`while`循環的另一個例子: ```java while (true){ statement(s); } ``` ## 示例:使用`while`循環迭代數組 這里我們使用`while`循環迭代并顯示數組元素。 ```java class WhileLoopExample3 { public static void main(String args[]){ int arr[]={2,11,45,9}; //i starts with 0 as array index starts with 0 too int i=0; while(i<4){ System.out.println(arr[i]); i++; } } } ``` **輸出:** ```java 2 11 45 9 ``` 查看這些相關的[程序](https://beginnersbook.com/2017/09/java-examples/): 1. [Java 程序:使用`while`循環顯示 Fibonacci 序列](https://beginnersbook.com/2017/09/java-program-to-display-fibonacci-series-using-loops/) 2. [Java 程序:使用`while`循環查找階乘](https://beginnersbook.com/2017/09/java-program-to-find-factorial-using-for-and-while-loop/)
                  <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>

                              哎呀哎呀视频在线观看