Kotlin When Expression
When statement is based on expression value. and inside of it's, possible to define multiple test cases based on expression value. Compiler are check test cases from top to bottom. when exist of test cases then executes this test case expression. This is similar to switch case of other programming languages. But this are not used break and default statement.
fun main(args: Array<String>) {
var number:Int =4
when(number){
0-> println("Zero")
1-> println("One")
2-> println("Two")
3-> println("Three")
4-> println("Four")
5-> println("Five")
else -> println("number is not 1 to 5")
}
}
Output
Four
else-statement is an option to when statement. it will work when define test cases is not matching. We are also used when statement as an expression of kotlin programming. In this case there is must be used on else statement. for example.
fun main(args: Array<String>) {
var number:Int =7
var info=when(number){
0-> "Zero"
1-> "One"
2-> "Two"
3-> "Three"
4-> "Four"
5-> "Five"
else -> "number is not 1 to 5"
}
println("$info")
}
Output
number is not 1 to 5
In this example when statement is execute else portion of inside test case. for example assume that there is not mention of else portion of this program.
fun main(args: Array<String>) {
var number:Int =7
var info=when(number){
0-> "Zero"
1-> "One"
2-> "Two"
3-> "Three"
4-> "Four"
5-> "Five"
}
println("$info")
}
Error
kotlin.kt:3:14: error: 'when' expression must be exhaustive, add necessary 'else' branch
var info=when(number){
^
Combine multiple test cases
Some of situation can be need to execute same block of statement into different test cases. In this case we are separated test case by using of comma.
fun main(args: Array<String>) {
var speed:Int =50
//example to combine multiple test case
when(speed){
10,20,30-> println("Slow")
40,50,60,70-> println("Average Speed")
80,90,100,110,120 ->println("High Speed")
else -> println("Speed is undefined")
}
}
Output
Average Speed
This program is work on limited test cases and constant values. so we can also defined the test cases between range of two values. see this example.
fun main(args: Array<String>) {
var speed:Int =120
//example of to set test case with range
when(speed){
in 1..40-> println("Slow")
in 41..80-> println("Average Speed")
in 81..150 ->println("High Speed")
else -> println("Speed is undefined")
}
}
Output
High Speed
When with no Expression
when there is not set any expression of when statement. then test case expression is decided which inner block of statement will be executed. For example.
fun main(args: Array<String>) {
val speed:Int =0
when{
(speed==0) -> println("Bike is not running")
(speed<60) -> println("Slow speed")
(speed<100)-> println("Average Speed")
(speed<130)-> println("High Speed")
else -> println("Bike is running, Speed is not defined")
}
}
Output
Bike is not running
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment