0Pricing
Kotlin Academy · Lesson

for Loop Over Ranges and Collections

Use for with IntRange, downTo, step, and collections.

for Loop Over Ranges and Collections is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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
}

Range with until (Exclusive)

until creates a half-open range that excludes the upper bound — useful for index loops.

fun main() {
    for (i in 0 until 4) {
        println("Index: $i")
    }
    // 0, 1, 2, 3
}

downTo for Counting Down

downTo iterates in reverse from start to end (inclusive).

fun main() {
    for (i in 10 downTo 1) {
        print("$i ")
    }
    println() // 10 9 8 7 6 5 4 3 2 1
}

step to Skip Values

Combine any range with step to take larger jumps between iterations.

fun main() {
    for (i in 0..20 step 5) {
        print("$i ")
    }
    println() // 0 5 10 15 20
}

Iterating a List

For loops work seamlessly with collections — iterate elements directly without indices.

fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    for (fruit in fruits) {
        println(fruit)
    }
}

Index-Based List Iteration

Use indices to get the positions, or withIndex() to get both index and value at once.

fun main() {
    val items = listOf("a", "b", "c")
    for (i in items.indices) {
        println("$i: ${items[i]}")
    }
    for ((idx, v) in items.withIndex()) {
        println("idx=$idx v=$v")
    }
}

Iterating a Map

Destructure each map entry into (key, value) pairs directly in the loop header.

fun main() {
    val scores = mapOf("Alice" to 92, "Bob" to 78)
    for ((name, score) in scores) {
        println("$name: $score")
    }
}

Iterating a String

Strings are iterable — each character flows through the loop.

fun main() {
    for (c in "Kotlin") {
        print("$c ")
    }
    println() // K o t l i n
}

Nested for Loops

You can nest loops to walk grids and matrices. Use multiple variables and inner ranges.

fun main() {
    for (row in 1..3) {
        for (col in 1..3) {
            print("($row,$col) ")
        }
        println()
    }
}

Range Over Characters

Character ranges work too — perfect for walking the alphabet.

fun main() {
    for (c in 'A'..'E') {
        print("$c ")
    }
    println() // A B C D E
}

Quick Check

Which range form excludes its upper bound?

Recap

Use for (x in ...) with ranges (.., until, downTo, step) or any iterable (list, map, string). Destructure map entries with (k, v); pair indices with withIndex().

Frequently asked questions

Is the “for Loop Over Ranges and Collections” lesson free?

Yes — the full text of “for Loop Over Ranges and Collections” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “for Loop Over Ranges and Collections”?

Use for with IntRange, downTo, step, and collections. You practise Kotlin 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 Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “for Loop Over Ranges and Collections” 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 Kotlin Academy lesson?

Yes. Every Kotlin 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

  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