Kotlin miniguide #3 Conditional

If can be used as an expression and its result assigned to a variable:


val a = 1

val b = 2

val msg = if (a > b ) "a greater than b" else "a less than b"

Kotlin doesn’t have a switch but there is something very cool called when:


val a = 1

when(a) {

    0 -> print("a is zero")

    in (1..10) ->print("a in range")

    else ->print("a not in range")

}

Lascia un commento