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
- Write one
whenexpression that returns a label. - Use a range in a loop and print the values.
- Write one note about when a loop should become a helper function.