<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Kotlin `while`和`do...while`循環 > 原文: [https://www.programiz.com/kotlin-programming/while-loop](https://www.programiz.com/kotlin-programming/while-loop) #### 循環在編程中用于重復特定的代碼塊。 在本文中,您將學習在 Kotlin 編程中創建`while`和`do...while`循環。 循環在編程中用于重復特定的代碼塊,直到滿足特定條件為止(測試表達式為`false`)。 循環使計算機成為有趣的機器。 想象一下,您需要在屏幕上打印一個句子 50 次。 好吧,您可以通過使用`print`語句 50 次(不使用循環)來做到這一點。 您需要打印一百萬次句子怎么樣? 您需要使用循環。 您將在示例的幫助下了解兩個循環`while`和`do..while`。 如果您熟悉 Java 中的 [`while`和`do...while`循環](/java-programming/do-while-loop "Java while and do...while Loop"),那么您也已經熟悉 Kotlin 中的這些循環。 * * * ## Kotlin `while`循環 `while`循環的語法為: ```kt while (testExpression) { // codes inside body of while loop } ``` * * * ### `while`循環如何工作? 括號內的測試表達式是[布爾型](/kotlin-programming/variable-types#boolean "Kotlin Boolean type")表達式。 如果測試表達式的計算結果為`true`, * `while`循環內的語句被執行。 * 然后,再次求值測試表達式。 該過程一直進行到測試表達式被求值為`false`為止。 如果測試表達式的計算結果為`false`, * `while`循環終止。 * * * ### `While`循環流程圖 ![Kotlin while Loop Flowchart](https://img.kancloud.cn/91/eb/91eb956d2c6b5e6776e12d74c719fd27_320x326.png) * * * ### 示例:Kotlin `while`循環 ```kt // Program to print line 5 times fun main(args: Array<String>) { var i = 1 while (i <= 5) { println("Line $i") ++i } } ``` 運行該程序時,輸出為: ```kt Line 1 Line 2 Line 3 Line 4 Line 5 ``` 注意,`while`循環內的`++i`語句。 在 5 次迭代之后,變量`i`將增加到 6。然后,將測試表達式`i <= 5`求值為`false`,然后循環終止。 * * * 如果循環的主體只有一個語句,則不必使用花括號`{ }`。 * * * ### 示例:計算自然數之和 ```kt // Program to compute the sum of natural numbers from 1 to 100. fun main(args: Array<String>) { var sum = 0 var i = 100 while (i != 0) { sum += i // sum = sum + i; --i } println("sum = $sum") } ``` 運行該程序時,輸出為: ```kt sum = 5050 ``` 在此,變量`sum`初始化為 0,并且`i`初始化為 100。在`while`循環的每次迭代中,變量`sum`被分配為`sum + i`,并且`i`的值減小 1,直到`i`等于 0。 ```kt 1st iteration: sum = 0+100 = 100, i = 99 2nd iteration: sum = 100+99 = 199, i = 98 3rd iteration: sum = 199+98 = 297, i = 97 ... .. ... ... .. ... 99th iteration: sum = 5047+2 = 5049, i = 1 100th iteration: sum = 5049+1 = 5050, i = 0 (then loop terminates) ``` 要了解有關測試表達式及其求值方式的更多信息,請訪問[比較](/kotlin-programming/operators#comparison-equality "Kotlin comparison Operators")和[邏輯運算符](/kotlin-programming/operators#logical "Kotlin Logical Operators")。 * * * ## Kotlin `do...while`循環 `do...while`循環類似于`while`循環,但有一個按鍵差異。 在檢查測試表達式之前,`do...while`循環的主體將執行一次。 其語法為: ```kt do { // codes inside body of do while loop } while (testExpression); ``` * * * ### `do...while`循環如何工作? `do`構造體內的代碼只執行一次(無需檢查`testExpression`)。 然后,檢查測試表達式。 如果將測試表達式求值為`true`,則執行循環體內的代碼,然后再次求值測試表達式。 該過程一直進行到測試表達式被求值為`false`為止。 當測試表達式求值為`false`時,`do..while`循環終止。 * * * ### `do...while`循環的流程圖 ![Kotlin do...while Loop flowchart](https://img.kancloud.cn/71/5c/715c30990d72641f697fb49cd24c0062_260x272.png) * * * ### 示例:Kotlin `do...while`循環 下面的程序計算用戶輸入的數字總和,直到用戶輸入 0。 要從用戶處獲取輸入,請使用`readline()`函數。 **推薦閱讀**: [Kotlin 基本輸入](/kotlin-programming/input-output#input) ```kt fun main(args: Array<String>) { var sum: Int = 0 var input: String do { print("Enter an integer: ") input = readLine()!! sum += input.toInt() } while (input != "0") println("sum = $sum") } ``` 當您運行程序時,輸出將類似于: ```kt Enter an integer: 4 Enter an integer: 3 Enter an integer: 2 Enter an integer: -6 Enter an integer: 0 sum = 3 ```
                  <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>

                              哎呀哎呀视频在线观看