Learning Kotlin – Part 4

Conditionals

This is a pretty quick one:

Kotlin has the standard Java if / else if / else statements

val age = 12

if (age < 5) {
    println("Go to kindergarten")
} else if (age == 5) {
    println("Go to pre-school")
} else if (age > 5 && age <= 17) {
    println("Go to grade ${age - 5}")
} else {
    println("Go to college!")
}

But one nice feature is the when clause:

It feels like a mixture of switch/case and if/else with a flavor of ranges 🙂

when (age) {
    0, 1, 2, 3, 4 -> println("Go to preschool")
    5 -> println("Go to Kindergarten")
    in 6..17 -> {
        val grade = age - 5
        println("Go to grade $grade")
    }
    else -> println("Go to college!")
}