what is kotlin when code example
Example 1: kotlin when
fun numberTypeName(x: Number) = when(x) {
0 -> "Zero" // Equality check
in 1..4 -> "Four or less" // Range check
5, 6, 7 -> "Five to seven" // Multiple values
is Byte -> "Byte" // Type check
else -> "Some number"
}
Example 2: when kotlin
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}