Range Operators
.. and until.
Range Operators is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
What Is a Range
A range represents a sequence of values between a start and an end.
Kotlin makes ranges first-class with the .. and until operators.
The .. Operator
The .. operator builds an inclusive range. Both ends are part of the range.
1..5 contains 1, 2, 3, 4, and 5.
fun main() {
val range = 1..5
println(range.toList())
}Checking Membership
Use in to test whether a value falls inside a range.
This is a clean replacement for two comparisons.
fun main() {
val x = 7
println(x in 1..10)
println(x in 1..5)
}Negated Membership
!in tests that a value is outside a range.
It reads more naturally than negating the whole expression.
fun main() {
val score = 105
if (score !in 0..100) {
println("out of range")
}
}The until Operator
until builds a range that excludes the upper bound.
1 until 5 contains 1, 2, 3, and 4. This is perfect for zero-based indexing.
fun main() {
val range = 1 until 5
println(range.toList())
}Inclusive vs Exclusive
The key difference:
0..nincludesn0 until nstops atn - 1
Choose based on whether the end should be part of the range.
fun main() {
println((0..3).toList())
println((0 until 3).toList())
}Ranges over Indices
until matches list indexing perfectly: a list of size n has indices 0 until n.
This avoids off-by-one errors.
fun main() {
val items = listOf("a", "b", "c")
for (i in 0 until items.size) {
println(i.toString() + ": " + items[i])
}
}Char Ranges
Ranges are not limited to numbers. Characters form ranges too, using their order.
'a'..'e' covers a, b, c, d, e.
fun main() {
println('a' in 'a'..'z')
println('5' in '0'..'9')
}Ranges as Values
A range is an object you can store in a variable and reuse.
This is handy for validation logic.
fun main() {
val validAge = 0..120
println(25 in validAge)
println(150 in validAge)
}Ranges in when
Ranges combine beautifully with when using in.
This expresses numeric buckets clearly.
fun grade(score: Int): Char = when (score) {
in 90..100 -> 'A'
in 80..89 -> 'B'
in 70..79 -> 'C'
else -> 'F'
}
fun main() {
println(grade(95))
println(grade(72))
}Putting It Together
Range operators build sequences and check membership:
..is inclusive of both endsuntilexcludes the upper endin/!intest membership- Works for numbers and characters
fun main() {
val c = 'k'
println(c in 'a'..'z')
val n = 50
println(n in 1 until 100)
}Quick Check
Test your understanding of range operators.
Recap
You learned the range operators:
..creates an inclusive rangeuntilexcludes the upper boundinand!intest membership- Ranges work for numbers and characters
Next you will control direction and step size with step and downTo.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Range Operators” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Range Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “Range Operators”?
.. and until. 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Range Operators” 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
- Range Operators
- Step and downTo
- Ranges in Loops
- Char and Custom Ranges