What is Kotlin “when” Statement?
As a beginner, you might be looking at how Kotlin switch statement works, especially if your background is working in C, Java, etc. languages.
In Kotlin, we have no switch statement. Instead, Kotlin replaces the switch with the “When” expression.
Syntax of how when is used in Kotlin:
fun main(args: Array<String>) { val num = 10 when (num) { 10 -> print("The number is 10") 20 -> print("The number is 20") else -> { // If none of the 'case' is true print("Some other number!") } } }
A few main points about Kotlin when:
- In Kotlin, the “when” can be used as an expression or statement.
- If using ‘when’ as an expression then the satisfied branch’s value becomes the value of the overall expression.
- In the case of using it as a statement, the values of the individual branches are ignored.
- Just like the ‘default’ case in Java switch, the default statement is ‘else’ in case of when in Kotlin.
- The else is mandatory if you use when as an expression.
Now, let us go through a few examples of using the when as an expression and statement.
An example of using when
In this example, we have a number variable and test the value in the ‘when’ and display a result. Five cases and an else branch are given as follows:
fun main(args: Array<String>) { val x = 20 when (x) { 5 -> print("The value is 5") 10 -> print("The value is 10") 15 -> print("The value is 15") 20 -> print("The value is 20") 25 -> print("The value is 25") else -> { print("Some other number!") } } }
The output:
Using commas for branch conditions example
You may also combine the branch conditions by a comma; in the situation having many cases. For example:
fun main(args: Array<String>) { val x = 5 when (x) { 5, 10, 15-> print("x == 5 or x ==10 or x == 15") 20, 25 -> print("x == 20 or x == 25") else -> { print("Some other number!") } } }
The result:
This can be useful if the intended output is the same for more than one branch.
The example of “when” as an expression
In this example, the when is used as an expression. As you run this code, the program will ask you to enter the color code from the given options.
If the entered code exists the corresponding branch value is returned and displayed, otherwise, the else part executes and displays its line:
fun main(args: Array<String>) { println("Enter the Color code from these bk, bl, re, ye") val col_val = readLine() val ret = when (col_val) { "bk" -> "Black" "bl" -> "Blue" "re" -> "Red" "ye" -> "Yellow" else -> "Please enter from the given code?" } println("$ret") }
The result as I entered ‘bk’:
An example of using a range in “when”
You may also use a range or a collection in the when in Kotlin. The example below uses a range with ‘in’ operator to check if the variable’s value is in the given range or not:
fun main(args: Array<String>) { val num = 75 when (num) { in 1..100 -> print("The number exists in the given range") else -> { // If none of the 'case' is true print("Number does not exist in the range!") } } }
The output:
Similarly, you may use the ‘!in’ operator to check the range or collection.
Using ‘when’ as if..else
If you use when without any expression, it acts as the if..else chain. In that case, the conditions are Boolean expression. See an example below:
fun main(args: Array<String>) { val num = 25 when { num == 5 -> print("The number is 5") num == 10 -> print("Number is 10") else -> print("Some other number!") } }
The output: