← Kotlin Programming learning pathKotlin Programming · Module 5 of 10
Control flow and when
Use if, when, loops and ranges.
📖 Key ideas
- if is an expression: val max = if (a > b) a else b.
- when replaces switch and can return values.
- Ranges: 1..10, 1 until 10, 10 downTo 1 step 2.
- for (i in list) loops over collections.
- when can check types: is String ->.
💡 Real-life example: when is a railway junction: the train (value) takes the track that matches its ticket.
whenMulti-branch expression
..Inclusive range
untilRange excluding the end
downToDescending range