Kotlin When Expression

Kotlin when expression is used when you have to match the value of an expression to a set of values and execute a block of statement corresponding to the matched value.

Kotlin when expression can be related to switch case in Java, but when expression is concise in syntax and extended in functionality.

In this tutorial, we will learn the syntax of Kotlin when expression, and how to use it in a Kotlin application with example programs.

Using object type of Any with when expression makes is really broad in the usage, where the matching can be done for int, string or any other primitive datatype. We shall learn in detail in our second example.

Syntax of Kotlin When statement

Let us see terminology and working of When expression.

Following is the syntax of Kotlin when expression.

</>
Copy
    when (expression) {
        value_1 -> {
            // set of statements
        }
        value_2 -> {
            // set of statements
        }
        value_n -> {
            // set of statements
        }
        else -> {
            // default set of statements
            // when no value matches the evaluated expression's value
        }
    }

In the above code snippet, the expression in the parenthesis next to the “when” keyword is evaluated to a value.

The value is matched against the values(value_1, value_2, . . ) written inside the block. When a match happens, the corresponding branch is executed.

If no match happens, and there is an else block is provided inside the when expression, the branch corresponding to the else block is executed.

Note : When there are more than one values that match to the expression’s value, only the branch corresponding to first match is executed. And the program control comes out of the when expression.

Example 1 – Kotlin When Expression

Following is a simple example for when expression in Kotlin. In the when expression, based on the value of n (nth day of week), we shall print the day of week.

Kotlin Program – example.kt

</>
Copy
fun main(args: Array) {
    printWeekDay(1);
    printWeekDay(2);
    printWeekDay(3);
    printWeekDay(4);
    printWeekDay(5);
    printWeekDay(6);
    printWeekDay(7);
    printWeekDay(12);
}

fun printWeekDay(n: Int){
    when (n) {
        1 -> {
            println("Sunday")
        }
        2 -> {
            println("Monday")
        }
        3 -> {
            println("Tuesday")
        }
        4 -> {
            println("Wednesday")
        }
        5 -> {
            println("Thursday")
        }
        6 -> {
            println("Friday")
        }
        7 -> {
            println("Saturday")
        }
        else -> { // Note the block
            println("Invalid value")
        }
    }
}

Run the above program in your IDE, and you shall get the following output printed to console.

Output

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Invalid value

For the function call, printWeekDay(12), there is no match found. Hence, else block is executed.

Example 2 – Kotlin When with object of type Any

In this example, the variable in the expression is of type “Any”. The variable can be matched with values of any type. The variable n, is matched against values of type Int and String. This could be extended to any of the primitive data types.

Kotlin Program – example.kt

</>
Copy
fun main(args: Array) {
    printWeekDay(1);
    printWeekDay("TUE");
}

fun printWeekDay(n: Any){
    when (n) {
        1 -> {
            println("Sunday")
        }
        "SUN" -> {
            println("Sunday")
        }
        2 -> {
            println("Monday")
        }
        "MON" -> {
            println("Monday")
        }
        3 -> {
            println("Tuesday")
        }
        "TUE" -> {
            println("Tuesday")
        }
        4 -> {
            println("Wednesday")
        }
        "WED" -> {
            println("Wednesday")
        }
    }
}

Run the Kotlin program and you shall get the following output in console.

Output

Sunday
Tuesday

Conclusion

In this Kotlin Tutorial, we have learnt the syntax of Kotlin When and how to use it in Kotlin for decision making tasks.