100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Ranges in Kotlin

Learn how Kotlin ranges express sequences of values for iteration and containment checks using .., until, downTo, and step.

Control FlowBeginner6 min readJul 8, 2026
Analogies

Introduction

Ranges in Kotlin represent a sequence of values, most commonly numbers or characters, defined by a start and an end point. They are used heavily with for loops for counted iteration, and with the in / !in operators to check whether a value falls within a given interval. Ranges are created using operators such as .. for an inclusive range, until for an exclusive upper bound, downTo for descending sequences, and step to control the increment.

🏏

Cricket analogy: A range like over 1..20 defines the powerplay-to-death-overs span used to loop through every over of an innings, just as Kotlin ranges iterate a bounded numeric sequence in a for loop.

Syntax

kotlin
1..5          // inclusive range: 1, 2, 3, 4, 5
1 until 5     // exclusive upper bound: 1, 2, 3, 4
5 downTo 1    // descending range: 5, 4, 3, 2, 1
1..10 step 2  // stepped range: 1, 3, 5, 7, 9

// containment checks
val isInRange = x in 1..10
val isOutside = x !in 1..10

Explanation

The .. operator creates a range that includes both endpoints, so 1..5 includes 5. When you need to exclude the upper bound, until is used instead, so 1 until 5 stops at 4 - handy for zero-based indexing scenarios like 0 until list.size. downTo builds a descending range, since .. only moves upward. step changes the increment between values in any of these ranges, and can be combined with downTo for a descending step. Ranges implement the in operator, so they can be used directly in for loops (for (i in 1..5)) or as fast containment checks (if (x in 1..10)) without needing to write manual comparisons.

🏏

Cricket analogy: 1..5 overs includes over 5 itself (like the full powerplay), while 1 until 5 stops before over 5, similar to zero-based indexing 0 until wickets.size iterating fielding positions without an out-of-bounds error; downTo counts down overs 20 downTo 1 in a run chase, and step 5 checks scores every fifth over.

Example

kotlin
fun main() {
    for (i in 1..5) print("$i ")
    println()

    for (i in 1 until 5) print("$i ")
    println()

    for (i in 5 downTo 1) print("$i ")
    println()

    for (i in 1..10 step 2) print("$i ")
    println()

    val x = 7
    println(x in 1..10)
    println(x !in 1..10)
}

// Output:
// 1 2 3 4 5
// 1 2 3 4
// 5 4 3 2 1
// 1 3 5 7 9
// true
// false

Key Takeaways

  • 1..5 creates an inclusive range from 1 to 5 (both ends included).
  • until creates a range that excludes the upper bound, e.g. 1 until 5 stops at 4.
  • downTo creates a descending range since .. only counts upward.
  • step controls the increment between values in any range.
  • Ranges support in / !in for concise containment checks and plug directly into for loops.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#RangesInKotlin#Ranges#Syntax#Explanation#Example#StudyNotes#SkillVeris