Kotlin - if...else 表示式



Kotlin if...else 表示式的工作方式與任何其他現代計算機程式語言中的 if...else 表示式類似。因此,讓我們從 Kotlin 中提供的傳統 if...else 語句開始。

語法

傳統 if...else 表示式的語法如下所示

if (condition) {
   // code block A to be executed if condition is true
} else {
  // code block B to be executed if condition is false
}

此處執行 if 語句並檢查給定的 condition。如果此條件計算結果為 true,則執行程式碼塊 A,否則程式進入 else 部分並執行程式碼塊 B。

示例

您可以嘗試以下示例

fun main(args: Array<String>) {
    val age:Int = 10

    if (age > 18) {
        print("Adult")
    } else {
        print("Minor")
    }
}

執行上述 Kotlin 程式時,將生成以下輸出

Minor

Kotlin if...else 表示式

Kotlin if...else 也可以用作表示式,該表示式返回值,並且此值可以分配給變數。以下是 Kotlin if...else 表示式的簡單語法

語法

val result = if (condition) {
   // code block A to be executed if condition is true
} else {
  // code block B to be executed if condition is false
}

如果您將 if 用作表示式,例如,為了返回值或將其分配給變數,則 else 分支是必須的。

示例

fun main(args: Array<String>) {
    val age:Int = 10

    val result = if (age > 18) {
        "Adult"
    } else {
        "Minor"
    }
    println(result)
}

執行上述 Kotlin 程式時,將生成以下輸出

Minor

if 只有一個語句時,您可以省略花括號 {}

fun main(args: Array<String>) {
    val age:Int = 10

    val result = if (age > 18) "Adult" else  "Minor"
    println(result)
}

執行上述 Kotlin 程式時,將生成以下輸出

Minor

您可以在 if...else 塊中包含多個語句,在這種情況下,最後一個表示式將作為塊的值返回。嘗試以下示例

fun main(args: Array<String>) {
    val age:Int = 10

    val result = if (age > 18) {
        println("Given condition is true")
        "Adult"
    } else {
        println("Given condition is false")
        "Minor"
    }
    print("The value of result : ")
    println(result)
}

執行上述 Kotlin 程式時,將生成以下輸出

Given condition is false
The value of result : Minor

Kotlin if...else...if 階梯

如果第一個條件為假,您可以使用 else if 條件來指定新的條件。

語法

if (condition1) {
  // code block A to be executed if condition1 is true
} else if (condition2) {
  // code block B to be executed if condition2 is true
} else {
  // code block C to be executed if condition1 and condition2 are false
}

示例

fun main(args: Array<String>) {
    val age:Int = 13

    val result = if (age > 19) {
        "Adult"
    } else if ( age > 12 && age  < 20 ){
        "Teen"
    } else {
        "Minor"
    }
    print("The value of result : ")
    println(result)
}

執行上述 Kotlin 程式時,將生成以下輸出

The value of result : Teen

Kotlin 巢狀 if 表示式

Kotlin 允許將一個 if 表示式放在另一個 if 表示式內部。這稱為 巢狀 if 表示式

語法

if(condition1) {
   // code block A to be executed if condition1 is true
   if( (condition2) {
      // code block B to be executed if condition2 is true
   }else{
      // code block C to be executed if condition2 is fals
   }
} else {
  // code block D to be executed if condition1 is false
}

示例

fun main(args: Array<String>) {
    val age:Int = 20 

    val result = if (age > 12) {
       if ( age > 12 && age  < 20 ){
           "Teen"
       }else{
           "Adult"
       }
    } else {
        "Minor"
    }
    print("The value of result : ")
    println(result)
}

執行上述 Kotlin 程式時,將生成以下輸出

The value of result : Adult

測驗時間 (面試及考試準備)

答案:D

解釋

所有提到的語句關於 Kotlin if 表示式都是正確的。

答案:B

解釋

Kotlin 不支援 if...then...else 語句。

Q 3 - 以下程式碼的輸出是什麼?

fun main(args: Array<String>) {

    var x = 20
    var y = 15
    var z = "Mango"

    val result = if (x > y ) {
       z = "Orange"
    } else {
       z = "Apple"
    }
    println("Value of result = $z")
}

A - Mango

B - Orange

C - Apple

D - 以上均不

答案:B

解釋

正確答案是 Orange,因為 x 大於 y,所以一旦此條件為真,它就會將 Orange 分配給 z 變數。

廣告

© . All rights reserved.