Control Flow (if, for, while)
Control flow is how a program decides what to do and how often to do it — Kotlin's if , for , and while let you branch on conditions and repeat work.
Learn Control Flow (if, for, while) in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A standout twist: in Kotlin if is an expression that returns a value, so it replaces the ternary operator entirely.
What You'll Learn in This Lesson
1️⃣ if/else — Statement and Expression
You can use if the familiar way — to run one block or another. But Kotlin goes further: if is an expression , so it produces a value you can assign. The value is the last expression of whichever branch runs. That is why Kotlin has no ? : ternary — if/else already covers it.
2️⃣ for Loops — Ranges, Collections, Indices
Kotlin's for loop always iterates over something: a range like 1..3 , a collection like a list, or an indexed view via withIndex() . There is no C-style for (i = 0; i < n; i++) — ranges replace it and read far more clearly.
3️⃣ while, do/while, break, and continue
A while loop repeats as long as its condition is true, checking before each pass. A do/while loop runs the body once and checks after . Inside any loop, break exits immediately and continue jumps to the next pass.
Now the same loop-control tools, plus a label to break out of an outer loop from deep inside a nested one:
Your turn. Fill in the ___ blanks, then run and compare.
The legendary interview warm-up. Combine a for loop, if/else , and the modulo operator % .
📋 Quick Reference — Control Flow
Practice quiz
In Kotlin, can if be used as an expression that returns a value?
- No, if is always a statement
- Only inside loops
- Yes, if/else returns the last expression of the chosen branch
- Only with a return keyword
Answer: Yes, if/else returns the last expression of the chosen branch. if is an expression in Kotlin, so val max = if (a > b) a else b works.
Why does Kotlin have no ternary (cond ? a : b) operator?
- Because if/else is already an expression that returns a value
- Because ternary is considered unsafe
- Because when replaces it
- It does have a ternary operator
Answer: Because if/else is already an expression that returns a value. if/else is an expression, so it covers what a ternary does in other languages.
When if is used as an expression assigned to a variable, the else branch is:
- Optional
- Forbidden
- Replaced by when
- Required so every path produces a value
Answer: Required so every path produces a value. The compiler must know what value to produce in every case, so else is required.
What does the range 1..3 produce when looped?
- 1, 2 (upper excluded)
- 1, 2, 3 (both endpoints included)
- 0, 1, 2
- 2, 3
Answer: 1, 2, 3 (both endpoints included). The .. operator builds a closed range that includes both endpoints.
What does 1 until 3 produce?
- 1, 2 (upper bound excluded)
- 1, 2, 3
- 0, 1, 2, 3
- 2, 3
Answer: 1, 2 (upper bound excluded). until builds a half-open range that excludes the upper bound.
Which loop checks its condition AFTER running the body once?
- while
- for
- do/while
- repeat
Answer: do/while. do/while runs the body once, then checks the condition.
Inside a loop, what does continue do?
- Exits the loop entirely
- Skips to the next iteration
- Restarts the whole loop
- Pauses execution
Answer: Skips to the next iteration. continue jumps to the next pass; break exits the loop.
How do you break out of an OUTER loop from inside a nested loop?
- Use break twice
- Use return
- It is not possible
- Use a label, e.g. break@outer
Answer: Use a label, e.g. break@outer. A label like outer@ lets you write break@outer to exit the outer loop.
Which is a valid Kotlin for loop over a list named fruits?
- for (i = 0; i < fruits.size; i++) { }
- for (fruit in fruits) { }
- foreach fruit in fruits { }
- for fruit of fruits { }
Answer: for (fruit in fruits) { }. Kotlin's for always iterates over something; there is no C-style for.
What does fruits.withIndex() give you in a for loop?
- Only the indices
- Only the values reversed
- Both the index and the value of each element
- A new sorted list
Answer: Both the index and the value of each element. withIndex() yields (index, value) pairs you can destructure.