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
- for Loop Over Ranges and Collections
- while and do-while: When to Use Each
- forEach, repeat, and forEachIndexed
- break, continue, and Labeled Returns