Kotlin Ranges

A range represents a sequence of values between two endpoints. Ranges are most useful in loops, in checks, and when branches. Kotlin supports ranges for integers, characters, and comparable types.

Creating Ranges

val intRange  = 1..10           // 1 to 10, both included
val charRange = 'A'..'Z'       // A to Z
val excluded  = 1 until 10     // 1 to 9 (10 excluded)

Visual: Closed vs Open Range


1..5   (closed / inclusive):
  ┌──┬──┬──┬──┬──┐
  │1 │2 │3 │4 │5 │
  └──┴──┴──┴──┴──┘
  ^ includes 5

1 until 5  (half-open / exclusive):
  ┌──┬──┬──┬──┐
  │1 │2 │3 │4 │
  └──┴──┴──┴──┘
  ^ stops before 5

Ranges in for Loops

for (i in 1..5) print("$i ")     // 1 2 3 4 5
for (i in 1 until 5) print("$i ") // 1 2 3 4

for (i in 5 downTo 1) print("$i ")  // 5 4 3 2 1
for (i in 0..20 step 4) print("$i ") // 0 4 8 12 16 20

Checking Membership with in

val score = 75

println(score in 60..100)   // true
println(score in 0..59)     // false
println(score !in 0..59)    // true

val letter = 'E'
println(letter in 'A'..'F') // true
println(letter in 'G'..'Z') // false

Ranges in when Expressions

val temperature = 28

val condition = when (temperature) {
    in Int.MIN_VALUE..0 -> "Freezing"
    in 1..15            -> "Cold"
    in 16..25           -> "Comfortable"
    in 26..35           -> "Warm"
    else                -> "Very hot"
}

println(condition)  // Warm

Character Range

for (ch in 'A'..'E') {
    print("$ch ")
}
// Output: A B C D E

println('C' in 'A'..'Z')   // true
println('c' in 'A'..'Z')   // false (lowercase 'c' not in uppercase range)

Range as an Object

A range is actually an object in Kotlin. You can store it in a variable and use it multiple times:

val passingRange = 50..100

val students = listOf(45, 78, 55, 32, 91, 49)

for (mark in students) {
    val result = if (mark in passingRange) "Pass" else "Fail"
    println("$mark → $result")
}

Output:

45 → Fail
78 → Pass
55 → Pass
32 → Fail
91 → Pass
49 → Fail

Range Functions

val r = 1..20

println(r.first)          // 1
println(r.last)           // 20
println(r.step)           // 1
println(r.count())        // 20
println(r.sum())          // 210
println(r.contains(15))   // true
println(r.toList())       // [1, 2, 3, ..., 20]

Stepped Ranges as Progressions

val evens = 2..20 step 2
val odds  = 1..19 step 2

println(evens.toList())  // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
println(odds.toList())   // [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Practical Example: Ticket Pricing

fun main() {
    val ages = listOf(5, 12, 17, 35, 62, 70)

    for (age in ages) {
        val price = when (age) {
            in 0..5   -> "Free"
            in 6..12  -> "₹50 (Child)"
            in 13..17 -> "₹100 (Teen)"
            in 18..59 -> "₹200 (Adult)"
            else      -> "₹150 (Senior)"
        }
        println("Age $age → $price")
    }
}

Output:

Age 5 → Free
Age 12 → ₹50 (Child)
Age 17 → ₹100 (Teen)
Age 35 → ₹200 (Adult)
Age 62 → ₹150 (Senior)
Age 70 → ₹150 (Senior)

Leave a Comment

Your email address will not be published. Required fields are marked *