if else in kotlin code example
Example 1: 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"
Example 2: kotlin if else
fun bigger(a: Int, b: Int) = if (a > b) a else b
Example 3: kotlin if else condition
if (a > b) {
max = a
} else {
max = b
}
Example 4: kotlin else if
if (a > b) {
// if
} else if (a == b) {
// else if
} else {
// else
}