HOWTO · Kotlin
How to Implement a Switch Case Statement in Kotlin
This article teaches how to write a Java-like switch-case statement in Kotlin. It will also show different ways in which we can use the Kotlin switch-case statement.
On this page
This article will introduce how to implement a switch case in Kotlin. The Kotlin language does not have any switch statement. Instead, it supports a much simpler when statement. It is the Kotlin equivalent to Java’s switch statement. The idea behind replacing the switch statement in Kotlin is to make the code easier to understand. But before getting into the when statement, let’s look at the use of the Java switch case.
Example code:
class Playground {
public static void main(String[] args) {
int n = 1;
switch (n) {
case 0:
System.out.println("Invalid");
break;
case 1:
System.out.println("Valid");
break;
default:
System.out.println("Number should be 0 or 1.");
break;
}
}
}
Output:
Valid
The switch statement reduces the code as we don’t have to use if...else if conditioning. Kotlin further simplifies this with the when statement. Here’s the code for using when in Kotlin for getting the same results as the previous Java example.
Example code:
fun main(args: Array<String>) {
val n = 1
when (n) {
0 -> print("Invalid")
1 -> print("Valid")
else -> {
print("Number should be 0 or 1.")
}
}
}
Output:
Valid
We can also remove the else part from the code if we are sure that the entered number will be from the given cases. The else in Kotlin when statement is like the default in Java switch statement. Besides using when as the standard statement, we can also use it for different purposes like giving multiple options in a single statement, branch conditioning without arguments, checking the type of a variable, etc. We will go through each of these cases.
Give Two or More Options to a when Statement in Kotlin
In the switch statement, we can match one case at a time with a given value. But while using when in Kotlin, we can provide multiple options to a single case by separating each with a comma. In the below example, we will print Valid for the numbers 1 and 2.
Example code:
fun main(args: Array<String>) {
val n = 2
when (n) {
0-> print("Invalid")
1, 2 -> print("1 and 2 are Valid")
}
}
Output:
1 and 2 are Valid
Apply Branch Conditions to when in Kotlin
With Kotlin when statements, we can also apply branch conditions. Suppose we need to match a case against a range of options; we can do that with the branch conditions. If the condition is true, the statement following the case is executed. Here’s a demonstration for the same.
Example code:
fun main(args: Array<String>) {
val n = 3
when (n) {
0 -> print("Invalid")
1 -> print("Invalid")
in 2..4 -> print("Numbers 2 through 4 are Valid")
}
}
Output:
Numbers 2 through 4 are Valid
Since the number was 3, it meets the conditions in 2..4. Hence, the condition’s print statement was shown as output. But what if two conditions are met? In that case, only the first true condition will be executed, and the rest will be ignored.
Example code:
fun main(args: Array<String>) {
val n = 3
when (n) {
0 -> print("Invalid")
1 -> print("Invalid")
in 2..4 -> print("Numbers 2 through 4 are Valid")
in 1..4 -> print("Numbers 1 through 4 are Valid")
}
}
Output:
Numbers 2 through 4 are Valid
Although both conditions are true, the compiler will execute only the print statement for the first condition in the above case.
Use when in Kotlin Without Arguments
We can also use the when expression without giving any arguments. It means that we can use the expression and then start writing the code block to execute. We can write conditional statements in the block. When a condition becomes true, the adjacent statement is executed. It is like running many conditional statements but executing only the first one that is true.
Example code:
fun main(args: Array<String>) {
val n = 3
when {
n < 4 -> print("Number is less than 4")
n > 4 -> print("Number is greater than 4")
}
}
Output:
Number is less than 4
Check the Data Type Using when Expression
By using the when expression in Kotlin, we can check the data type of the provided variable. It is done with the help of the conditional branching method. We will use the is condition that helps type check a variable.
Example code:
fun main(args: Array<String>) {
var n: Any = 2
when(n){
is Int -> println("Integer")
is String -> println("String")
is Double -> println("Double")
}
}
Output:
Integer