Ranges in Loops
Iterate cleanly.
Ranges in Loops is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
Ranges Power Loops
Ranges and progressions are the natural way to drive for loops in Kotlin.
There is no classic C-style for(i=0; i<n; i++); you iterate over a range instead.
Basic Counting Loop
Loop over an inclusive range to count from a start to an end.
The variable takes each value in turn.
fun main() {
for (i in 1..5) {
print(i.toString() + " ")
}
println()
}Exclusive Loops with until
For zero-based counting, until stops one short of the bound.
This is the standard way to loop over a known count.
fun main() {
for (i in 0 until 4) {
print(i.toString() + " ")
}
println()
}Looping by Index
Use indices to loop over the valid positions of a collection.
It is shorthand for 0 until size.
fun main() {
val items = listOf("x", "y", "z")
for (i in items.indices) {
println(i.toString() + " -> " + items[i])
}
}Counting Down
Combine downTo with a loop to count backward, perfect for countdowns.
fun main() {
for (i in 5 downTo 1) {
print(i.toString() + " ")
}
println("liftoff")
}Stepping Through
Add step to iterate with a custom increment, like every other value.
fun main() {
for (i in 0..20 step 5) {
print(i.toString() + " ")
}
println()
}Looping over Characters
Char ranges let you loop through letters or digits directly.
fun main() {
for (c in 'a'..'e') {
print(c)
}
println()
}withIndex for Value and Position
When you need both the index and the element, withIndex pairs them.
Destructure the pair right in the loop header.
fun main() {
val items = listOf("a", "b", "c")
for ((index, value) in items.withIndex()) {
println(index.toString() + ": " + value)
}
}Nested Range Loops
Nest loops over ranges to build grids or tables.
Each combination of the two variables is visited.
fun main() {
for (row in 1..3) {
for (col in 1..3) {
print((row * col).toString() + "\t")
}
println()
}
}Accumulating in a Loop
A range loop is great for accumulating a result, like a running sum.
fun main() {
var sum = 0
for (i in 1..100) {
sum += i
}
println(sum)
}Putting It Together
Ranges make loops clean and expressive:
..anduntilfor countingindicesfor collection positionsdownToandstepfor direction and stridewithIndexfor value plus position
fun main() {
val data = listOf(10, 20, 30)
for (i in data.indices) {
println("item " + i + " = " + data[i])
}
}Quick Check
Test your understanding of ranges in loops.
Recap
You learned to loop with ranges:
..,until,indicesfor countingdownToandstepfor direction and stridewithIndexfor index plus value
Next you will explore char ranges and custom range types.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Ranges in Loops” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Ranges in Loops”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “Ranges in Loops”?
Iterate cleanly. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Ranges in Loops” 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
- Range Operators
- Step and downTo
- Ranges in Loops
- Char and Custom Ranges