In Kotlin, a range is the sequence of values defined by start and end values. Both values are included in the rage.
How to Create a Range in Kotlin?
There are 3 ways to create a range:
- Using the .. operator
- rangeTo() function
- downTo() function
Using the .. Operator:
fun main() {
val oneToFive = 1..5
for (number in oneToFive) {
println(number)
}
}
Output:
1
2
3
4
5
Instead of creating oneToFive, we can directly use the in operator with for loop.
fun main() {
for (number in 1..5) {
println(number)
}
}
For characters:
fun main() {
for (char in 'a'..'c') {
println(char)
}
}
Output:
a
b
c
For Long values:
fun main() {
for (longValue in 0L..2L) {
println(longValue)
}
}
Output:
0
1
2
rangeTo() function:
The function takes a parameter called other and creates a range up to that value.
fun main() {
for (number in 1.rangeTo(other = 3)) {
println(number)
}
}
Output:
1
2
3
Along with Integer, the function works with Byte, Short, and Long values also.
For character range:
fun main() {
for (ch in 'a'.rangeTo(other = 'c')) {
println(ch)
}
}
Output:
a
b
c
downTo() function:
It is similar to rangeTo() method, but it creates a range in the descending order.
fun main() {
// numbers
for (number in 3.downTo(to = 1)) {
println(number)
}
// characters
for (ch in 'c'.downTo(to = 'a')) {
println(ch)
}
}
Output:
3
2
1
c
b
a
Note: In the downTo(), the start and end values can be of different types.
Here are the different overloads Kotlin provides:
- Byte.downTo(to: Byte)
- Byte.downTo(to: Short)
- Byte.downTo(to: Int)
- Byte.downTo(to: Long)
- Short.downTo(to: Byte)
- Short.downTo(to: Short)
- Short.downTo(to: Int)
- Short.downTo(to: Long)
- Int.downTo(to: Byte)
- Int.downTo(to: Short)
- Int.downTo(to: Int)
- Int.downTo(to: Long)
- Long.downTo(to: Byte)
- Long.downTo(to: Short)
- Long.downTo(to: Int)
- Long.downTo(to: Long)
Instead of downTo(), we can use the Kotlin reversed() function.
fun main() {
val fiveToOne = (1..5).reversed()
for (number in fiveToOne) {
println(number)
}
}
Output:
5
4
3
2
1
By default, the values in the sequence are increased/decreased by 1. But, what if we want to change it? We can do that by using the step function.
Kotlin step Function:
The step function is used to provide the distance between values in a sequence.
fun main() {
println("""step with .. """)
for (number in 1..10 step 3) {
println(number)
}
println("""step with rangeTo() """)
for (number in 1.rangeTo(5) step 2) {
println(number)
}
println("""step with downTo() """)
for (number in 5.downTo(0) step 4) {
println(number)
}
}
Output:
step with ..
1
4
7
10
step with rangeTo()
1
3
5
step with downTo()
5
1
Check If a Number Exists:
We can use the in operator to check if a number exists in the range. It returns true if the condition is true.
fun main() {
val oneToFour = 1..4
if (2 in oneToFour) {
println("2 Exists")
} else {
println("2 Doesn't Exist")
}
}
Output:
2 Exists
Similarly, we have !in. It returns true if the condition fails.
fun main() {
val oneToFour = 1..10
if (20 !in oneToFour) {
println("20 Doesn't Exist")
} else {
println("20 Exist")
}
}
Output:
20 Doesn't Exist
Get First, Last, and Step Values in a Kotlin Range:
We can use the first, last, and step properties to get the corresponding values in a range.
fun main() {
// Get the first value
println((1..5).first) // prints 1
// Get the last value
println((1..5).last) // prints 5
// Get the step value
println((1..5).step) // prints 1
}
This is all about Kotlin Range. I hope you understood the concept. If you have any doubts, leave a comment below.
Continue Learning Kotlin: