In Kotlin, a range is a collection of values defined by a start and an end point. Ranges are commonly used to represent a sequence of numbers or characters, and they are inclusive, meaning both the start and end values are part of the range. By default, the step (the gap between consecutive values) is 1. Ranges in Kotlin work with comparable types such as numbers and characters.
Creating Ranges in Kotlin
There are three ways for creating Range in Kotlin
- Using the .. operator
- Using rangeTo() function
- Using downTo() function
(..) operator
This is the simplest and most commonly used way to create a range. The .. operator creates a range from the starting value to the ending value, including both. It works for both numbers and characters.
Example - Integer Range:
Kotlin
fun main() {
println("Integer range:")
for (i in 1..5) {
println(i)
}
}
Output:
Integer range:
1
2
3
4
5
Example - Character Range:
Kotlin
fun main() {
println("Character range:")
for (ch in 'a'..'e') {
println(ch)
}
}
Output:
Character range:
a
b
c
d
e
rangeTo() function
The rangeTo() function works exactly like the .. operator. In fact, a..b is just a shorthand for a.rangeTo(b).
Example - Integer Range:
Kotlin
fun main() {
println("Integer range:")
for (i in 1.rangeTo(5)) {
println(i)
}
}
Output:
Integer range:
1
2
3
4
5
Example - Character Range:
Kotlin
fun main() {
println("Character range:")
for (ch in 'a'.rangeTo('e')) {
println(ch)
}
}
Output:
Character range:
a
b
c
d
e
downTo() function
The downTo() function creates a range in reverse order, going from a larger value to a smaller one. Like the other methods, it works with numbers and characters.
Example - Integer Range:
Kotlin
fun main() {
println("Integer range in descending order:")
for (i in 5 downTo 1) {
println(i)
}
}
Output:
Integer range in descending order:
5
4
3
2
1
Example - Character Range:
Kotlin
fun main() {
println("Character range in reverse order:")
for (ch in 'e' downTo 'a') {
println(ch)
}
}
Output:
Character range in reverse order:
e
d
c
b
a
Range using forEach loop
We can also use the forEach loop to go through the values in a range.
Kotlin
fun main() {
println("Integer range:")
(2..5).forEach { println(it) }
}
Output:
Integer range:
2
3
4
5
step() Function
The step() function lets us control the gap between values in the range. By default, the step is 1, but we can set it to any positive integer. The step value cannot be 0.
Example:
Kotlin
fun main() {
for (i in 3..11 step 2) {
print("$i ")
}
}
Output:
3 5 7 9 11
reversed() function
It is used to reverse the given range type. Instead of downTo() we can use reverse() function to print the range in descending order.
Kotlin
fun main() {
for (i in (2..8).reversed()) {
print("$i ")
}
}
Output:
8 7 6 5 4 3 2
Some predefined function in range
In Kotlin, Ranges also provide several useful functions -
- min() – Returns the smallest value in the range
- max() – Returns the largest value
- sum() – Adds up all the values in the range
- average() – Returns the average value
Kotlin
fun main() {
val range = 15..20
println("The minimum value of range is: ${range.min()}")
println("The maximum value of range is: ${range.max()}")
println("The sum of all values of range is: ${range.sum()}")
println("The average value of range is: ${range.average()}")
}
Output:
The minimum value of range is: 15
The maximum value of range is: 20
The sum of all values of range is: 105
The average value of range is: 17.5
Checking If a Value Is in a Range
We can use the in and !in operators to check if a value lies within a range.
Kotlin
fun main() {
val range = 5..10
val value = 2
if (value in range) {
println("$value lies within the range")
} else {
println("$value does not lie within the range")
}
}
Output:
2 does not lie within the range
Explore
Overview
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges