A loop repeats code. The for loop repeats it once for every item in something you can loop over: a range of numbers, a list, a string and more.
Page Contents
for with a range
1..5 is a range from 1 to 5, including both ends:
fun main() {
for (i in 1..5) {
println("Lap $i")
}
}Output:
Lap 1 Lap 2 Lap 3 Lap 4 Lap 5
until and ..< (exclude the end)
until and the newer ..< operator stop before the last number. They’re useful with indexes, which start at 0:
fun main() {
for (i in 0 until 3) print("$i ")
println()
for (i in 0..<3) print("$i ")
println()
}Output:
0 1 2 0 1 2
step: skip numbers
fun main() {
for (i in 0..20 step 5) print("$i ")
println()
}Output:
0 5 10 15 20
downTo: count backwards
fun main() {
for (i in 5 downTo 1) print("$i ")
println("Liftoff!")
for (i in 10 downTo 0 step 3) print("$i ")
println()
}Output:
5 4 3 2 1 Liftoff! 10 7 4 1
for (i in 5..1) does nothing, because 5..1 is an empty range. To count down, you must use downTo.
Looping over a list
fun main() {
val fruits = listOf("Apple", "Mango", "Banana")
for (fruit in fruits) {
println(fruit)
}
}Output:
Apple Mango Banana
Getting the index too
fun main() {
val fruits = listOf("Apple", "Mango", "Banana")
for ((index, fruit) in fruits.withIndex()) {
println("$index: $fruit")
}
for (i in fruits.indices) {
println("Item ${i + 1} of ${fruits.size}: ${fruits[i]}")
}
}Output:
0: Apple 1: Mango 2: Banana Item 1 of 3: Apple Item 2 of 3: Mango Item 3 of 3: Banana
Looping over a string
fun main() {
var vowels = 0
for (ch in "Kotlin Course") {
if (ch.lowercaseChar() in "aeiou") vowels++
}
println("Vowels: $vowels")
}Output:
Vowels: 5
Looping over a map
fun main() {
val prices = mapOf("Tea" to 20, "Coffee" to 35)
for ((item, price) in prices) {
println("$item costs ₹$price")
}
}Output:
Tea costs ₹20 Coffee costs ₹35
repeat: run something N times
When you don’t need the counter, repeat is shorter:
fun main() {
repeat(3) {
println("Hello")
}
repeat(3) { i -> print("Try ${i + 1} ") }
println()
}Output:
Hello Hello Hello Try 1 Try 2 Try 3
Nested loops
fun main() {
for (row in 1..3) {
for (col in 1..3) {
print("${row * col}\t")
}
println()
}
}Output:
1 2 3 2 4 6 3 6 9
Common mistakes
- Counting down with
..:5..1is empty. Use5 downTo 1. - Index out of bounds:
for (i in 0..list.size)goes one too far. Uselist.indicesor0 until list.size. - Trying to change the loop variable:
iis read-only inside aforloop. Use awhileloop if you need to control the counter yourself.
Exercise
Print the multiplication table of 7, then the sum of all even numbers from 1 to 100.
fun main() {
for (i in 1..10) println("7 x $i = ${7 * i}")
var sum = 0
for (n in 2..100 step 2) sum += n
println("Sum of even numbers 1–100: $sum")
}Output:
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 Sum of even numbers 1–100: 2550
FAQ
What is the difference between .. and until in Kotlin?
1..5 includes 5. 1 until 5 (or 1..<5) stops at 4.
How do I loop backwards in Kotlin?
Use downTo: for (i in 10 downTo 1). Add step to skip values.
How do I get the index in a Kotlin for loop?
Use for ((i, item) in list.withIndex()), or loop over list.indices.
