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
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..10Explanation
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
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
// falseKey Takeaways
1..5creates an inclusive range from 1 to 5 (both ends included).untilcreates a range that excludes the upper bound, e.g.1 until 5stops at 4.downTocreates a descending range since..only counts upward.stepcontrols the increment between values in any range.- Ranges support
in/!infor concise containment checks and plug directly into for loops.
Practice what you learned
1. What values does the range `1..5` include?
2. Which range expression excludes the upper bound?
3. How would you create a descending range from 10 down to 1?
4. What does `1..10 step 2` produce?
5. What does the expression `x in 1..10` do?
Was this page helpful?
You May Also Like
Loops in Kotlin (for, while, do-while)
Understand Kotlin's for, while, and do-while loops, along with break, continue, and labeled loops.
when Expression in Kotlin
Explore Kotlin's when expression, a powerful and flexible replacement for Java's switch statement.
Operators in Kotlin
Explore Kotlin's arithmetic, comparison, logical, range, and containment operators, including the == vs === distinction.
Collections in Kotlin
An overview of how Kotlin groups objects into Lists, Sets, and Maps using its dual mutable/read-only collection model.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics