Kotlin - When Expression

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 Kotlin 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. Kotlin when expression evaluates a section of code among many alternatives as explained in below example.
Kotlin when matches its argument against all branches sequentially until some branch condition is satisfied.
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 -> "Invalid day."
   }
   println(result)
}

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

Kotlin when can be used either as an expression or as a statement, simply like 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.
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("Invalid day.")
   }
}

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

We can combine multiple when conditions into a single condition.
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 Kotlin program, it will generate the following output:
Weekday

Kotlin ranges are created using double dots .. and we can use them while checking when condition with the help of in operator.
fun main(args: Array<String>) {
   val day = 2

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

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

Kotlin when can use arbitrary expressions instead of a constant as branch condition.
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 Kotlin program, it will generate the following output:
y + z = x = 20

Kotlin when braches can be put as block of code enclosed within curly braces.
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("Invalid day.")
   }
}

When you run the above Kotlin program, it will generate the following output:
Second day of the week
Tuesday

Q 1 - Which of the following is true about Kotlin when expression?
A - It is used to compare a single value against multiple conditions
B - Kotlin when expression can be used in place of if..else if expression
C - Kotlin when branches can be integer, string, array or ranges
D - All of the above
All the mentioned statements are correct about Kotlin when expression.
Q 2 - Kotlin when can be used as an expression as well as a statement?
A - True
B - False
Yes it is true that Kotlin when can be used as an expression as well as a statement.
Q 3 - Kotlin when is inspired by which of the following Java statement
A - switch statement
B - if statement
C - do...while statement
C - None of the above
Kotlin when has very similar syntax and functionality like switch statement available in Java.

    68 Lectures 
     4.5 hours 

 Arnab Chakraborty

    71 Lectures 
     5.5 hours 

 Frahaan Hussain

    18 Lectures 
     1.5 hours 

 Mahmoud Ramadan

    49 Lectures 
     6 hours 

 Catalin Stefan

    49 Lectures 
     2.5 hours 

 Skillbakerystudios

    22 Lectures 
     1 hours 

 CLEMENT OCHIENG
 Print
 Add Notes
 Bookmark this page