Kotlin Nested Loops
A nested loop places one loop inside another. The outer loop controls the big steps, and the inner loop repeats its own work for every big step. Nested loops draw patterns, build tables, and walk through grids of data.
The Clock Idea
Look at a clock. The minute hand completes a full circle for every single move of the hour hand. The hour hand is the outer loop. The minute hand is the inner loop. Nothing moves the hour hand until the minute hand finishes its circle.
Outer loop (hours) Inner loop (minutes) ------------------- -------------------- hour 1 minute 1, 2, 3 hour 2 minute 1, 2, 3 hour 3 minute 1, 2, 3
Basic Syntax
fun main() {
for (hour in 1..2) {
for (minute in 1..3) {
println("Hour $hour, minute $minute")
}
}
}The program prints six lines. The inner loop runs three times for hour 1, and three more times for hour 2.
Hour 1, minute 1 Hour 1, minute 2 Hour 1, minute 3 Hour 2, minute 1 Hour 2, minute 2 Hour 2, minute 3
Counting the Work
Multiply the loop sizes to find the total number of steps. Two rows and three columns produce six steps. A loop of 1,000 inside a loop of 1,000 produces one million steps. Large nested loops slow a program, so choose sizes with care.
Drawing a Rectangle of Stars
Rows come from the outer loop and columns come from the inner loop. The print function keeps stars on one line, and println() ends the row.
fun main() {
for (row in 1..3) {
for (col in 1..5) {
print("* ")
}
println()
}
}
* * * * * * * * * * * * * * *
Drawing a Triangle
The inner loop can depend on the outer loop. Row 1 prints one star, row 2 prints two stars, and row 3 prints three stars.
fun main() {
for (row in 1..4) {
for (col in 1..row) {
print("* ")
}
println()
}
}
* * * * * * * * * *
Multiplication Table
A table shows the power of nested loops. Each row holds one multiplier, and each column holds another.
fun main() {
for (i in 1..3) {
for (j in 1..5) {
print("${i * j}\t")
}
println()
}
}
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15
Walking a Grid
A seating chart in a cinema has rows and seats. Nested loops visit every seat once.
Seat1 Seat2 Seat3
Row A [ ] [ ] [ ]
Row B [ ] [ ] [ ]
Row C [ ] [ ] [ ]
fun main() {
val rows = listOf("A", "B", "C")
for (row in rows) {
for (seat in 1..3) {
println("Seat $row$seat is free")
}
}
}Stopping Early with Labels
A plain break leaves only the inner loop. A label gives the outer loop a name, so one command can leave both loops. Put the label name followed by @ before the loop, and write break@name inside.
fun main() {
outer@ for (i in 1..3) {
for (j in 1..3) {
if (i * j == 4) {
println("Found at $i and $j")
break@outer
}
println("$i x $j = ${i * j}")
}
}
}The program stops the moment the product reaches 4. Neither loop continues.
Tips for Clean Nested Loops
- Pick meaningful names such as
rowandcolinstead ofiandjwhen the loops describe a grid. - Keep the inner body short. Move long work into a function.
- Limit nesting to two or three levels. Deeper nesting hurts readability.
- Use the underscore name
_for a counter you never read.
