Signup/Sign In

Kotlin when Expression

In this tutorial, we will discuss the Kotlin when expression. Kotlin when expression is used when we have to choose a particular statement based on different values of a variable. It is similar to switch case in other programming languages.

A simple program to print the type of alphabet using when expression is:

fun main() {
    val alphabet:Char = 'a'
    when(alphabet){
        'a' -> println("Character is vowel")
        'e' -> println("Character is vowel")
        'i' -> println("Character is vowel")
        'o' -> println("Character is vowel")
        'u' -> println("Character is vowel")
        else -> println("Character is consonant")
    }
}


Character is vowel

In the above example:

  • Variable alphabet is passed inside the when expression.

  • We define different cases to match with input variable by adding -> symbol in front of them.

  • If there is no match, then the else block will be executed.

Similar to if-else expression, the when expression also returns a value. So, we can return a value and assign it to a variable. Let us understand this with an example.

We will write a program to find the day based on the number of the day:

fun main() {
    val number:Int = 5
    val day:String = when(number){
        1 -> "Monday"
        2 -> "Tuesday"
        3 -> "Wednesday"
        4 -> "Thursday"
        5 -> "Friday"
        6 -> "Saturday"
        7 -> "Sunday"
        else -> "Invalid number"
    }
    println("Day is: $day")
}


Day is: Friday

In the code above, we have specified conditions to match the value of the variable number. So if the value of the variable number will be 1, then "Monday" will get added to the day variable, for 2, "Tuesday" will get added to the day variable, and so on. If the value is 8 or above, then the else expression gets executed.

Now that we have a basic introduction to the Kotlin when expression, let's see more ways of using it.

Block of code in when expression

A block of code can also be placed inside the Kotlin when expression conditions:

fun main() {
    val number:Int = 5
    when(number){
        1 -> {
            println("Hello")
            println("Monday")
        }
        2 -> {
            println("Hello")
            println("Tuesday")
        }
        3 -> {
            println("Hello")
            println("Wednesday")
        }
        4 -> {
            println("Hello")
            println("Thursday")
        }
        5 -> {
            println("Hello")
            println("Friday")
        }
        6 -> {
            println("Hello")
            println("Saturday")
        }
        7 -> {
            println("Hello")
            println("Sunday")
        }
        else -> {
            println("Invalid input!!")
        }
    }
}


Hello
Friday

Just like in the code example above we can have whatever logic we want to have in these when expression conditions.

Combine multiple when conditions

We can combine multiple conditions with commas in a when expression.

fun main() {
    val rating: Int = 5
    val review: String = when(rating){
        10 -> "Excellent"
        8,9 -> "Above Average"
        6,7 -> "Average"
        4,5 -> "Below Average"
        1,2,3 -> "Poor"
        else -> "Invalid input"

    }
    println("Review is: $review")
}


Review is: Below Average

In the code example above we have combined multiple conditions together.

Summary

In this tutorial, we covered the when expression in Kotlin. It can is very usefule while writing Kotlin code where you have to create a menu like feature. In the next tutorial, we will cover different type of loops in Kotlin.



About the author:
I'm a writer at studytonight.com.