when
when replaces switch. It can match values, ranges, types and conditions.
Example
fun describe(x: Any) = when (x) {
1, 2 -> "small"
in 3..9 -> "medium"
is String -> "text"
else -> "other"
}
println(describe(2))
println(describe(5))
println(describe("hi"))Exercise
Default branch.
-> "other"Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.