0Pricing
Kotlin Academy · Lesson

Step and downTo

Control progression.

Step and downTo is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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:

  • step sets the increment
  • downTo counts 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:

  • step changes the increment
  • downTo produces a descending sequence
  • The step value is always positive
  • They can be combined and iterated

Next you will use ranges to write clean loops.

Frequently asked questions

Is the “Step and downTo” lesson free?

Yes — the full text of “Step and downTo” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Range Operators
  2. Step and downTo
  3. Ranges in Loops
  4. Char and Custom Ranges
← Back to Kotlin Academy