Control Flow
Use if/else expressions, when statements, for and while loops to control program logic in Kotlin.
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")
}All lessons in this course
- Variables & Data Types
- Functions & Lambdas
- Control Flow