English 中文(简体)
Kotlin - When Expression
  • 时间:2024-11-03

Kotpn - When Expression


Previous Page Next Page  

Consider a situation when you have large number of conditions to check. Though you can use if..else if expression to handle the situation, but Kotpn provides when expression to handle the situation in nicer way. Using when expression is far easy and more clean in comparison to writing many if...else if expressions. Kotpn when expression evaluates a section of code among many alternatives as explained in below example.

Kotpn when matches its argument against all branches sequentially until some branch condition is satisfied.

Kotpn when expression is similar to the switch statement in C, C++ and Java.

Example

fun main(args: Array<String>) {
   val day = 2

   val result = when (day) {
     1 -> "Monday"
     2 -> "Tuesday"
     3 -> "Wednesday"
     4 -> "Thursday"
     5 -> "Friday"
     6 -> "Saturday"
     7 -> "Sunday"
     else -> "Invapd day."
   }
   println(result)
}

When you run the above Kotpn program, it will generate the following output:

Tuesday

Kotpn when as Statement

Kotpn when can be used either as an expression or as a statement, simply pke a switch statement in Java. If it is used as an expression, the value of the first matching branch becomes the value of the overall expression.

Example

Let s write above example once again without using expression form:

fun main(args: Array<String>) {
   val day = 2

   when (day) {
     1 -> println("Monday")
     2 -> println("Tuesday")
     3 -> println("Wednesday")
     4 -> println("Thursday")
     5 -> println("Friday")
     6 -> println("Saturday")
     7 -> println("Sunday")
     else -> println("Invapd day.")
   }
}

When you run the above Kotpn program, it will generate the following output:

Tuesday

Combine when Conditions

We can combine multiple when conditions into a single condition.

Example

fun main(args: Array<String>) {
   val day = 2

   when (day) {
     1, 2, 3, 4, 5 -> println("Weekday")
     else -> println("Weekend")
   }
}

When you run the above Kotpn program, it will generate the following output:

Weekday

Range in when Conditions

Kotpn ranges are created using double dots .. and we can use them while checking when condition with the help of in operator.

Example

fun main(args: Array<String>) {
   val day = 2

   when (day) {
     in 1..5 -> println("Weekday")
     else -> println("Weekend")
   }
}

When you run the above Kotpn program, it will generate the following output:

Weekday

Expression in when Conditions

Kotpn when can use arbitrary expressions instead of a constant as branch condition.

Example

fun main(args: Array<String>) {
   val x = 20
   val y = 10
   val z = 10
   
   when (x) {
      (y+z) -> print("y + z = x = $x")
      else -> print("Condition is not satisfied")
   }
}

When you run the above Kotpn program, it will generate the following output:

y + z = x = 20

Kotpn when with block of code

Kotpn when braches can be put as block of code enclosed within curly braces.

Example

fun main(args: Array<String>) {
   val day = 2

   when (day) {
     1 -> {
        println("First day of the week")
        println("Monday")
     }
     2 -> {
        println("Second day of the week")
        println("Tuesday")
     }
     3 -> {
        println("Third day of the week")
        println("Wednesday")
     }
     4 -> println("Thursday")
     5 -> println("Friday")
     6 -> println("Saturday")
     7 -> println("Sunday")
     else -> println("Invapd day.")
   }
}

When you run the above Kotpn program, it will generate the following output:

Second day of the week
Tuesday

Quiz Time (Interview & Exams Preparation)

Q 1 - Which of the following is true about Kotpn when expression?

Answer : D

Explanation

All the mentioned statements are correct about Kotpn when expression.

Q 2 - Kotpn when can be used as an expression as well as a statement?

Answer : A

Explanation

Yes it is true that Kotpn when can be used as an expression as well as a statement.

Q 3 - Kotpn when is inspired by which of the following Java statement

Answer : A

Explanation

Kotpn when has very similar syntax and functionapty pke switch statement available in Java.

Advertisements