Ranges & Progressions
A range is an interval of values with a start and an end, written with the .. operator — Kotlin uses ranges for looping, for testing membership with in , and inside when .
Learn Ranges & Progressions in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Add step , downTo , or until and a range becomes a flexible, readable replacement for clumsy counter loops.
What You'll Learn in This Lesson
1️⃣ Building Ranges: .., until, downTo
The .. operator makes a closed range that includes both endpoints. The until keyword makes a half-open range that stops one short of the top — perfect for indices. The downTo keyword counts the other direction.
2️⃣ step and Character Ranges
A range with step becomes a progression that jumps by more than one each pass. The step value is always positive — direction comes from .. or downTo . Because characters are ordered, you can also build ranges of letters.
3️⃣ in / !in and Ranges in when
The in operator asks "is this value inside the range?" and !in asks the opposite — both return a clean boolean. This makes range checks read almost like English, and they pair beautifully with when for bucketing a value into categories.
Ranges inside when are the idiomatic way to grade or classify a number:
Your turn. Fill in the ___ blanks, then run and compare.
Use downTo to count from 5 to 1, then announce liftoff.
📋 Quick Reference — Ranges
Practice quiz
Which values does the closed range 1..5 include?
- 1, 2, 3, 4
- 1, 2, 3, 4, 5
- 2, 3, 4, 5
- 0, 1, 2, 3, 4
Answer: 1, 2, 3, 4, 5. The .. operator makes a closed range that includes both endpoints.
Which values does 1 until 5 produce?
- 1, 2, 3, 4, 5
- 2, 3, 4, 5
- 1, 2, 3, 4
- 1, 5
Answer: 1, 2, 3, 4. until is half-open: it excludes the upper bound, giving 1, 2, 3, 4.
What does 5 downTo 1 produce?
- 1, 2, 3, 4, 5
- An empty range
- 5, 4, 3, 2, 1
- 5, 1
Answer: 5, 4, 3, 2, 1. downTo counts downward from 5 to 1.
What does the step in 0..10 step 2 control?
- The direction of iteration
- The size of the increment between values
- The starting value
- Whether the end is included
Answer: The size of the increment between values. step changes the increment; 0..10 step 2 yields 0, 2, 4, 6, 8, 10.
What does the expression 7 in 1..5 evaluate to?
- true
- false
- 7
- It does not compile
Answer: false. in tests membership; 7 is not within 1..5, so the result is false.
What does writing 5..1 (with ..) produce?
- 5, 4, 3, 2, 1
- An empty range
- 1, 2, 3, 4, 5
- A compile error
Answer: An empty range. A .. range counting downward is empty; use 5 downTo 1 to count down.
Why must the step value always be positive, even with downTo?
- Negative steps are slower
- Direction comes from .. or downTo; step is only the magnitude
- Kotlin rounds negatives to positives
- It is a historical bug
Answer: Direction comes from .. or downTo; step is only the magnitude. Direction is decided by .. or downTo; a non-positive step throws IllegalArgumentException.
Which is the cleanest way to loop over all valid indices of a list?
- 0..list.size
- 1..list.size
- for (i in list.indices)
- list.size downTo 0
Answer: for (i in list.indices). list.indices is a built-in range of every valid index, avoiding off-by-one errors.
Can you iterate a Double range like 0.0..1.0 in a for loop?
- Yes, with a default step of 0.1
- Yes, always
- No, Doubles support in checks but not for-loop iteration
- Only with downTo
Answer: No, Doubles support in checks but not for-loop iteration. Double ranges support in membership tests but cannot be iterated in a for loop.
Where do ranges make grading logic especially readable?
- Inside a when expression with in branches
- Only inside while loops
- In string templates
- In class constructors
Answer: Inside a when expression with in branches. Ranges with in inside a when keep classification logic flat and readable.