if expression kotlin code example
Example 1: when kotlin
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
Example 2: kotlin if else condition
if (a > b) {
max = a
} else {
max = b
}
Example 3: kotlin if else
fun bigger(a: Int, b: Int) = if (a > b) a else b
Example 4: kotlin else if
if (a > b) {
// if
} else if (a == b) {
// else if
} else {
// else
}