Range Operators
.. and until.
Range Operators is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Range Operators” lesson free?
Yes — the full text of “Range Operators” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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