0Pricing
Kotlin Academy · Lesson

forEach, repeat, and forEachIndexed

Explore functional-style iteration helpers and when they beat classic loops.

Functional Iteration

Beyond for, Kotlin offers higher-order iteration: forEach, forEachIndexed, and repeat. They make small loops more expressive.

forEach Basics

forEach takes a lambda and runs it for every element. The element is implicitly named it.

fun main() {
    val items = listOf("a", "b", "c")
    items.forEach { println(it) }
}

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