0Pricing
Kotlin Academy · Lesson

Ranges in Loops

Iterate cleanly.

Ranges Power Loops

Ranges and progressions are the natural way to drive for loops in Kotlin.

There is no classic C-style for(i=0; i<n; i++); you iterate over a range instead.

Basic Counting Loop

Loop over an inclusive range to count from a start to an end.

The variable takes each value in turn.

fun main() {
    for (i in 1..5) {
        print(i.toString() + " ")
    }
    println()
}

All lessons in this course

  1. Range Operators
  2. Step and downTo
  3. Ranges in Loops
  4. Char and Custom Ranges
← Back to Kotlin Academy