Kotlin Conditionals – when statement and when expression

In the previous post, we’ve seen if statements and if expressions in Kolin. Now we’ll see how to use the Kotlin conditionals – when statements and when expressions in our programs. If you come from a Java background (or C family for that matter. C, C++, Java etc), you’ll be aware of a conditional construct called switch. The switch statement is similar to multiple if…else statements and actually, in those languages, the switch statement is less powerful than if…else ladders. Kotlin has this nice feature: when statements and expression.

Visit the Kotlin official documentation for control flow here.

When statements in Kotlin are somewhat similar to switch in Java but it’s more powerful as you’ll see shortly.

fun main(args: Array<String>) {
    val ch = 3
    when (ch) {
        1 -> {
            println("Value is 1")
            println("checked first block")
        }

        2 -> {
            println("Value is 2")
            println("checked second block")
        }
        3 -> {
            println("Value is 3")
            println("checked third block")
        }
        else -> {
            println("This is executed when a true condition is not found")
        }
    }
}

This shows the basic usage of when statement. It checks for a true condition and when a true condition is found, the corresponding block is executed. Notice the else keyword at the end. It’s executed when no true condition could be found.

As with if statements, if there’s only one statement for a condition check, then the curly braces are optional, you can make your code really concise by removing the curly braces.
Like this:

fun main(args: Array<String>) {
    val ch = 3
    when (ch) {
        1 -> println("Value is 1")

        2 -> println("Value is 2")

        3 -> println("Value is 3")

        else -> println("This is executed when a true condition is not found")

    }
}

Now, how’s it better than the switch statement in Java? With the switch statement, we can compare only equality. We can never test for example, for a condition like, if the number is within a particular range. In Kotlin, it’s totally possible.
See this:

fun main(args: Array<String>) {
    val ch = 3
    when (ch) {
        in 1..3 -> println("Value is within the range 1 to 3")

        in 3..6 -> println("Value is within the range 3 to 6")

        in 6..9 -> println("Value is within the range 6 to 9")

        else -> println("This is executed when a true condition is not found")

    }
}

The .. (two dots) operator is for denoting a range in Kotlin. 1..5 means, in the range from 1 to 5.

When-Expression in Kotlin

Like if, when can also be used as an expression in Kotlin. See this example:

fun main(args: Array<String>) {
    val ch = 850
    val qrange = when (ch) {
        in 1..300 -> "Low Range"

        in 300..600 -> "Medium Range"

        in 600..1000 -> "High Range"

        else -> "Invalid value"

    }
    println(qrange)
}

It will output “High Range” to the screen.
the value qrange is assigned various values according to various conditions. If no conditions are met, then the compiler checks the else block and assigns that value to qrange. In this case, the condition will be met in the third check.

Exercises:

  • Make a calculator program using when. Present user with a menu to choose from the four arithmetic operations. The user makes a choice. The program then asks for two numbers and carries out the operation. And finally, prints the output. (Give the choice manually for now. We’ll see how to accept user inputs later.

That’s all about Kotlin when statement and when expression. If you have any doubt or if you think I missed some important points, please do let me know by adding a comment below.
Feedback is always welcome
Happy coding 🙂

« Back to the index of Kotlin tutorial

Please follow and like us:

Leave a Reply