Kotlin 'When' statement is almost same as the 'if -- else' statement or the Switch - Case statement we use in Java.
Let us understand Kotlin 'When' statement with the below example.
Say you are a class teacher and there are only 5 students in your class.
And you have marked them with their Roll Numbers and their Names.
Roll Number | Name |
---|---|
1 | Ronald |
2 | John |
3 | Murali |
4 | Satish |
5 | Debasish |
Now, say the Principal of the school has asked you to write a Kotlin program, that will show you the name of a student once you enter his/her roll number.
Now, that you are an expert in 'if --- else'. You wrote the program using 'if --- else'.
fun main() { var rollNumber = 2 if (rollNumber == 1) { println("Ronald") } else if (rollNumber == 2) { println("John") } else if (rollNumber == 3) { println("Murali") } else if (rollNumber == 4) { println("Satish") } else if (rollNumber == 5) { println("Debasish") } else { println("The student does not exist.") } }
So, you wanted to search the name of the student whose roll number is 2.
And you got the output as 'John'.
Now, just think for a moment. What if there were 100 students in your class? You had to write 100 'if -- else -- if' statements.
Well! The good news is Kotlin provides a replacement for the above case. Where the same variable needs to be compared with several values.
What I meant is, the same variable 'rollNumber' is repeated at every 'if -- else -- if' statements.
And to avoid this repetition, Kotlin 'When' statement comes to rescue.
So, at first, let us rewrite the above program using Kotlin 'When' statement. Then we will understand how it works?
fun main() { var rollNumber = 2 when (rollNumber) { 1 -> println("Ronald") 2 -> println("John") 3 -> println("Murali") 4 -> println("Satish") 5 -> println("Debasish") else -> println("The student does not exist.") } }
Now, if you look at the current code. It is quite cleaner, compared to the 'if -- else -- if' code.
There is a 'when' statement, where we specify the 'rollNumber'.
Then there are numbers, like 1, 2 e.t.c. followed by '->'
1 -> println("Ronald") 2 -> println("John") 3 -> println("Murali") 4 -> println("Satish") 5 -> println("Debasish") else -> println("The student does not exist.")
And for every case, it checks for the 'rollNumber'(2 in this case as 'rollNumber=2').
When it finds a match in 2.
2 -> println("John")
It prints "John" and comes out of the 'when' statement.
when(expression/variable) { value1 -> // code for value1 value2 -> // code for value2 else -> // code for default block }
The 'else' block is also an optional block,
else -> println("The student does not exist.")
The 'default' block is only executed none of the cases matches. And is exactly same as,
else -> println("The student does not exist.")
For the 'if -- else -- if' code.
fun main() { var rollNumber = 10 when (rollNumber) { 1 -> println("Ronald") 2 -> println("John") 3 -> println("Murali") 4 -> println("Satish") 5 -> println("Debasish") else -> println("The student does not exist.") } }
In the above code, we are checking for the student whose roll number is 10. And since roll number 10 does not exist, the 'else' block is executed.
We have already seen in 'Kotlin when', the example of you being a teacher.
Now, we will look at the same example,
Say you are a class teacher and there are only 5 students in your class.
And you have marked them with their Roll Numbers and their Names.
Roll Number | Name |
---|---|
1 | Ronald |
2 | John |
3 | Murali |
4 | Satish |
5 | Debasish |
Now, say the Principal of the school has asked you to write a Kotlin program, that will show you the roll number of a student once you enter his/her name.
fun main() { var name = "John" when (name) { "Ronald" -> println("His roll number is 1") "John" -> println("His roll number is 2") "Murali" -> println("His roll number is 3") "Satish" -> println("His roll number is 4") "Debasish" -> println("His roll number is 5") else -> println("The student does not exist.") } }
This time we are trying to match a String in the 'when' statement.
We have taken the name in a String variable 'name' and initialised it with "John".
Then we have taken the name and put it in switch.
when (name) { "Ronald" -> println("His roll number is 1") "John" -> println("His roll number is 2") "Murali" -> println("His roll number is 3") "Satish" -> println("His roll number is 4") "Debasish" -> println("His roll number is 5") else -> println("The student does not exist.") }
And checked which case matches "John".
And found
"John" -> println("His roll number is 2")
So printed John's roll number