Kotlin When
when replaces switch and is much more powerful. It can match values, ranges, types and conditions, and it returns a value.
Example
val day = 3
val name = when (day) {
1 -> "Mon"
2 -> "Tue"
3 -> "Wed"
else -> "Other"
}
println(name)
println(when (15) { in 1..9 -> "small"; in 10..99 -> "medium"; else -> "large" })Exercise
Fallback branch.
when (x) { -> 0 }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.