Beginner 6 min readKotlin 2.0

Kotlin Ranges โ€” .. , until, downTo, and step

Ranges represent a sequence of values between two endpoints. Kotlin ranges work with numbers, characters, and many collection operations.

What You Will Learn

  • Inclusive range with ..
  • Exclusive range with until
  • Reverse range with downTo
  • Step values
  • Check membership with in
  • Character ranges

Inclusive Range with ..

1..5 creates a range from 1 to 5, including both endpoints.

Inclusive Range

kotlin
fun main() {
    val range = 1..5
    for (n in range) print("$n ")
    println()
    println(3 in range)  // containment check
    println(6 in range)
}
Output
1 2 3 4 5 true false

1..5 includes 1, 2, 3, 4, 5. The in operator checks if a value is within the range.

Exclusive Range with until

1 until 5 creates a range from 1 to 4 (5 excluded).

until Range

kotlin
fun main() {
    for (i in 0 until 5) print("$i ")
    println()
    // Equivalent using indices
    val list = listOf("a","b","c")
    for (i in 0 until list.size) print("${list[i]} ")
}
Output
0 1 2 3 4 a b c

until is useful for list indices: 0 until list.size gives valid indices without going out of bounds.

downTo and step

downTo creates a descending range. step controls the increment.

downTo and step

kotlin
fun main() {
    for (i in 10 downTo 1 step 3) print("$i ")
    println()
    for (c in 'a'..'e') print("$c ")
}
Output
10 7 4 1 a b c d e

10 downTo 1 step 3 counts down: 10, 7, 4, 1. Character ranges work the same as number ranges.

Practice Exercise

Exercisepredict output

What prints? for (i in 1..10 step 3) print("$i ")

Quick Quiz

Quick Quiz

What is the difference between 1..5 and 1 until 5?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-01Kotlin 2.0

Written by KotlinGuide Editorial Team ยท Reviewed by KotlinGuide Technical Review