📏 Kotlin – Ranges: Work with Number Sequences Easily
🧲 Introduction – Why Learn Kotlin Ranges?
Ranges in Kotlin provide a simple way to represent sequences of values, typically numbers or characters. Whether you’re looping through numbers, checking if a value lies within a range, or creating slices—Kotlin’s range expressions make the task readable and concise.
🎯 In this guide, you’ll learn:
- How to define and use numeric and character ranges
- Use ranges in
for
loops and condition checks - Customize ranges using
step
,downTo
, anduntil
- Real-world use cases and best practices
🔢 Kotlin Range Syntax
✅ Standard Range using ..
val range = 1..5
Represents numbers: 1, 2, 3, 4, 5
The ..
operator is inclusive.
🔁 Using Ranges in Loops
for (i in 1..3) {
println(i)
}
🟢 Output:
1
2
3
🔁 Reverse Ranges using downTo
for (i in 5 downTo 1) {
println(i)
}
🟢 Output:
5
4
3
2
1
- Useful for countdowns or reverse iterations.
🔁 Excluding End with until
for (i in 1 until 5) {
println(i)
}
🟢 Output:
1
2
3
4
until
excludes the end value (5
in this case).
⏩ Skipping Steps with step
for (i in 2..10 step 2) {
println(i)
}
🟢 Output:
2
4
6
8
10
step
defines how much to increment/decrement.
🔤 Character Ranges
Kotlin also supports character ranges.
for (ch in 'a'..'e') {
println(ch)
}
🟢 Output:
a
b
c
d
e
✔️ Works similarly to numeric ranges.
📌 Range Checks with in
and !in
val x = 10
if (x in 1..20) {
println("x is in range")
}
if (x !in 15..30) {
println("x is NOT in the second range")
}
🟢 Output:
x is in range
x is NOT in the second range
📐 Range Types Summary
Expression | Description | Inclusive | Reversible | Step |
---|---|---|---|---|
a..b | Inclusive range from a to b | ✅ | ❌ | ✅ |
a downTo b | Reverse from a to b | ✅ | ✅ | ✅ |
a until b | Range from a to b-1 | ❌ | ❌ | ✅ |
🧠 Best Practices for Ranges
Practice | Why It Helps |
---|---|
Use until to avoid off-by-one | Prevents logic errors in loops |
Combine downTo with step | Enhances control in countdowns |
Use in for readable conditionals | Replaces verbose >= and <= checks |
Prefer ranges in for loops | Cleaner than indexed iterations |
🚫 Common Mistakes
❌ Mistake | ✅ Fix |
---|---|
Using .. instead of until | Use until to exclude upper bound |
Forgetting step with custom logic | Add step to control iteration |
Assuming a..b works with Float | Use IntRange —floats aren’t supported |
📌 Summary – Recap & Next Steps
Kotlin ranges give you a powerful and readable way to handle sequences, loops, and conditional logic. Whether you’re looping through numbers or checking value bounds, ranges help keep your code clean, expressive, and bug-free.
🔍 Key Takeaways:
- Use
..
for inclusive ranges anduntil
to exclude the end. - Combine with
step
for flexible iteration control. - Use
in
/!in
for intuitive range-based checks. - Works with both numbers and characters.
⚙️ Practical Use:
Perfect for loop counters, character parsing, pagination, input validation, or mathematical operations in Android and server-side Kotlin.
❓ FAQs – Kotlin Ranges
❓ What is the difference between ..
and until
in Kotlin?
✅ ..
includes the end value, until
excludes it.
1..5 → 1, 2, 3, 4, 5
1 until 5 → 1, 2, 3, 4
❓ Can Kotlin ranges be used with Double
or Float
?
✅ Not directly. Kotlin ranges support integral and character types only. Use step
or manual loops for floating-point.
❓ How do I loop backwards in Kotlin?
✅ Use downTo
:
for (i in 5 downTo 1) { println(i) }
❓ Can I check if a character is in a range?
✅ Yes:
if ('b' in 'a'..'z') { println("It's a letter") }
❓ How do I skip items in a range?
✅ Use step
:
for (i in 1..10 step 2) { println(i) }
Share Now :