Answers for "what is when statement in kotlin"

12

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"
Posted by: Guest on May-10-2020
2

kotlin when

fun signAsString(x: Int)= when {
 x < 0 -> "Negative"
 x == 0 -> "Zero"
 else -> "Positive"
}
Posted by: Guest on January-15-2021

Code answers related to "what is when statement in kotlin"

Browse Popular Code Answers by Language