← Kotlin Programming learning path
Kotlin Programming · Module 5 of 10

Control flow and when

Use if, when, loops and ranges.

📖 Key ideas

  1. if is an expression: val max = if (a > b) a else b.
  2. when replaces switch and can return values.
  3. Ranges: 1..10, 1 until 10, 10 downTo 1 step 2.
  4. for (i in list) loops over collections.
  5. 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