Control Flow
Use if/else expressions, when statements, for and while loops to control program logic in Kotlin.
Control Flow is a free Android Academy lesson on CoddyKit. This is lesson 3 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 3 lessons in total.
Control Flow Overview
Control flow lets your program make decisions and repeat actions.
- if/else — choose between paths
- when — match a value against multiple cases
- for — repeat over a range or collection
- while — repeat while a condition is true
if/else as an Expression
In Kotlin, if/else is an expression — it returns a value, so you can assign it directly:
fun main() {
val score = 75
// Traditional style
if (score >= 60) {
println("Pass")
} else {
println("Fail")
}
// Expression style
val grade = if (score >= 90) "A"
else if (score >= 75) "B"
else if (score >= 60) "C"
else "F"
println("Grade: $grade")
}The when Expression
when is Kotlin's powerful replacement for switch/case:
- Matches against any value, not just integers
- Can be used as an expression (returns a value)
- The
elsebranch catches everything else
when in Action
Use when to handle multiple cases cleanly:
fun main() {
val day = 3
val name = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4, 5 -> "Thu or Fri" // multiple values
6, 7 -> "Weekend"
else -> "Unknown"
}
println(name) // Wednesday
// when with a range
val temp = 28
val feel = when {
temp < 10 -> "Cold"
temp < 25 -> "Comfortable"
else -> "Hot"
}
println(feel) // Hot
}for Loops
Use for to iterate over ranges, lists, and other collections:
1..5— range from 1 to 5 (inclusive)1 until 5— range from 1 to 4 (exclusive end)10 downTo 1— counting down1..10 step 2— skip every other value
for Loop Examples
Iterate over ranges and collections:
fun main() {
// Range
for (i in 1..5) print("$i ") // 1 2 3 4 5
println()
// Until (excludes end)
for (i in 0 until 3) print("$i ") // 0 1 2
println()
// Step
for (i in 0..10 step 3) print("$i ") // 0 3 6 9
println()
// List
val fruits = listOf("Apple", "Banana", "Cherry")
for (fruit in fruits) println(fruit)
}while & do-while
while repeats as long as a condition is true. do-while runs the body at least once before checking the condition.
while Loop Example
Count down using while:
fun main() {
var count = 5
while (count > 0) {
println("Count: $count")
count--
}
println("Done!")
// do-while runs body first
var x = 0
do {
println("x = $x")
x++
} while (x < 3)
}break & continue
Control loop execution with:
break— exit the loop immediatelycontinue— skip this iteration and go to the next
Kotlin also supports labeled breaks for nested loops: break@outer.
Quick Check
What does 1 until 5 produce?
Recap: Control Flow
You now know how to control program flow in Kotlin:
if/elseas an expression — assign results directlywhen— clean replacement for switch/caseforwith ranges,until,step,downTowhileanddo-while
Next: Kotlin's killer feature — null safety.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 36
- Lessons
- 152
Frequently Asked Questions
Is the “Control Flow” lesson free?
Yes — any 3 lessons of the Android Academy learning path, including “Control Flow”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 3 lessons in total.
What will I learn in “Control Flow”?
Use if/else expressions, when statements, for and while loops to control program logic in Kotlin. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.
How long does the “Control Flow” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Variables & Data Types
- Functions & Lambdas
- Control Flow