Step and downTo
Control progression.
Step and downTo is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 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.
Controlling Progressions
A plain range steps by one in increasing order. Kotlin lets you change both the step size and the direction.
The tools are step and downTo, which produce progressions.
The step Function
step changes how much the value increases each time.
0..10 step 2 yields 0, 2, 4, 6, 8, 10.
fun main() {
val progression = 0..10 step 2
println(progression.toList())
}Step That Skips the End
A step may not land exactly on the upper bound. The progression simply stops at the last value within range.
1..10 step 3 ends at 10? No, it ends at the largest reachable value.
fun main() {
val progression = 1..10 step 3
println(progression.toList())
}The downTo Function
downTo builds a descending progression.
5 downTo 1 yields 5, 4, 3, 2, 1.
fun main() {
val countdown = 5 downTo 1
println(countdown.toList())
}Why Not Just Reverse a Range
A normal range like 5..1 is empty because the start is greater than the end.
To count down you must use downTo.
fun main() {
println((5..1).toList()) // empty
println((5 downTo 1).toList())
}Combining downTo and step
You can add step to a descending progression as well.
The step is always a positive number; downTo handles the direction.
fun main() {
val progression = 10 downTo 0 step 2
println(progression.toList())
}Step Must Be Positive
The step value must be positive, even when counting down. A zero or negative step throws an exception.
Direction is the job of .. versus downTo, not the step sign.
fun main() {
try {
val bad = 0..10 step 0
println(bad.toList())
} catch (e: IllegalArgumentException) {
println("step must be positive")
}
}Progressions Are Iterable
A progression can be looped over directly. This is the most common use.
It produces each value in turn without building a list.
fun main() {
for (i in 10 downTo 1 step 3) {
print(i.toString() + " ")
}
println()
}Inspecting a Progression
A progression knows its first, last, and step values.
Note that last may differ from the requested end when the step skips it.
fun main() {
val p = 1..10 step 3
println(p.first)
println(p.last)
println(p.step)
}until with step
until also accepts a step, giving an exclusive upper bound plus a custom increment.
This is handy for sampling indices.
fun main() {
val p = 0 until 10 step 2
println(p.toList())
}Putting It Together
Progressions give you full control over iteration:
stepsets the incrementdownTocounts downward- Step is always positive
- They combine and are directly iterable
fun main() {
for (i in 100 downTo 0 step 25) {
print(i.toString() + " ")
}
println()
}Quick Check
Test your understanding of step and downTo.
Recap
You learned to control progressions:
stepchanges the incrementdownToproduces a descending sequence- The step value is always positive
- They can be combined and iterated
Next you will use ranges to write clean loops.
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 “Step and downTo” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Step and downTo”, 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 “Step and downTo”?
Control progression. 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 2 of 4.
How long does the “Step and downTo” 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