for Loop Over Ranges and Collections
Use for with IntRange, downTo, step, and collections.
Kotlin for Loops
Kotlin's for loop iterates over anything that provides an iterator — ranges, arrays, lists, maps, sets, even strings.
For Loop Over a Range
Use .. to build an inclusive integer range. The loop runs from start to end inclusive.
fun main() {
for (i in 1..5) {
println(i)
}
// 1, 2, 3, 4, 5
}All lessons in this course
- for Loop Over Ranges and Collections
- while and do-while: When to Use Each
- forEach, repeat, and forEachIndexed
- break, continue, and Labeled Returns