”when” is nothing but ‘Switch’ statement in C and Java language.There is also default case but this is expressed with else in When block.There is no break statement.We can attach multiple cases as well as a range(like 1…20) in one case.Also we can attach multiple condition in one case.The appropriate example is written below-
fun main( args: Array<String>){
// when as expression
val x=2
when (x){
0,1,4 -> println(“x is one”)
2 -> {
println(“x is two”)
println(“Hello”)
}
3 -> println(“x is three”)
in 5…20 -> println(“x is between 5 to 20”)
else{
println(“x is unknown”)
println(“I don’t know the value of x”)
}
}
}
Output:
x is two
Hello
Android Developer at Openweb Solutions