Kotlin Control

Kotlin control flow is expression-oriented. Branching often returns values instead of only performing actions.

if and when

val message = if (count > 0) "positive" else "zero or negative"

val category = when (count) {
    0 -> "none"
    in 1..3 -> "small"
    else -> "large"
}

Loops and ranges

for (i in 1..3) {
    println(i)
}

Break and continue

Use breaks carefully and keep loop exit conditions easy to read. Kotlin ranges often make loops clearer than index-heavy code.

Practice

  1. Write one when expression that returns a label.
  2. Use a range in a loop and print the values.
  3. Write one note about when a loop should become a helper function.