<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 `if`表達式 > 原文: [https://www.programiz.com/kotlin-programming/if-expression](https://www.programiz.com/kotlin-programming/if-expression) #### 在本文中,您將借助示例學習在 Kotlin 中使用`if`表達式。 ## `if...else`的傳統用法 `if...else`的語法是: ```kt if (testExpression) { // codes to run if testExpression is true } else { // codes to run if testExpression is false } ``` 如果將`testExpression`求值為`true`,則`if`執行代碼的特定部分。 它可以具有可選的`else`子句。 如果`testExpression`為假,則執行`else`子句中的代碼。 * * * ### 示例:`if...else`的傳統用法 ```kt fun main(args: Array<String>) { val number = -10 if (number > 0) { print("Positive number") } else { print("Negative number") } } ``` 運行該程序時,輸出為: ```kt Negative number ``` * * * ## Kotlin `if`表達式 與 Java(以及其他許多編程語言)不同,`if`可以在 Kotlin 中用作表達式; 它返回一個值。 **推薦閱讀**: [Kotlin 表達式](/kotlin-programming/statement-expression#expressions "Kotlin Expression") * * * 這是一個例子: ### 示例:Kotin `if`表達式 ```kt fun main(args: Array<String>) { val number = -10 val result = if (number > 0) { "Positive number" } else { "Negative number" } println(result) } ``` 運行該程序時,輸出為: ```kt Negative number ``` 當使用`if`作為表達式時,`else`分支是必需的。 * * * 如果`if`的主體只有一個語句,則花括號是可選的。 例如, ```kt fun main(args: Array<String>) { val number = -10 val result = if (number > 0) "Positive number" else "Negative number" println(result) } ``` 這類似于 Java 中的[三元運算符](/java-programming/ternary-operator "Java ternary operator.")。因此,在 Kotlin 中沒有三元運算符。 * * * ### 示例:具有多個表達式的`if`塊 如果`if`分支的塊包含多個表達式,則最后一個表達式作為該塊的值返回。 ```kt fun main(args: Array<String>) { val a = -9 val b = -11 val max = if (a > b) { println("$a is larger than $b.") println("max variable holds value of a.") a } else { println("$b is larger than $a.") println("max variable holds value of b.") b } println("max = $max") } ``` 運行該程序時,輸出為: ```kt -9 is larger than -11. max variable holds value of a. max = -9 ``` **推薦閱讀**: *Kotlin `when`語句* * * * ## Kotlin `if..else..if`階梯 您可以使用`if..else...if`階梯在 Kotlin 中的許多塊中返回一個代碼塊。 * * * ### 例如:`if..else..if`階梯 ```kt fun main(args: Array<String>) { val number = 0 val result = if (number > 0) "positive number" else if (number < 0) "negative number" else "zero" println("number is $result") } ``` 該程序檢查`num`是正數,負數還是零。 * * * ## Kotlin 嵌套`if`表達式 一個`if`表達式可以位于另一個`if`表達式的嵌套塊內。 * * * ### 示例:嵌套`if`表達式 該程序計算三個數字中最大的數字。 ```kt fun main(args: Array<String>) { val n1 = 3 val n2 = 5 val n3 = -2 val max = if (n1 > n2) { if (n1 > n3) n1 else n3 } else { if (n2 > n3) n2 else n3 } println("max = $max") } ``` 運行該程序時,輸出為: ```kt max = 5 ```
                  <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>

                              哎呀哎呀视频在线观看