kotlin when and code example
Example 1: kotlin when
fun signAsString(x: Int)= when {
x < 0 -> "Negative"
x == 0 -> "Zero"
else -> "Positive"
}
Example 2: Kotlin when
val x = 3
when(x) {
3 -> println("yes")
8 -> println("no")
else -> println("maybe")
}
// when can also be used as an expression!
val y = when(x) {
3 -> "yes"
8 -> "no"
else -> "maybe"
}
println(y) // "yes"