0Pricing
Kotlin Academy · Lesson

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

  1. for Loop Over Ranges and Collections
  2. while and do-while: When to Use Each
  3. forEach, repeat, and forEachIndexed
  4. break, continue, and Labeled Returns
← Back to Kotlin Academy