Introduction
In simple terms, Range in Kotlin is a collection of finite values defined by starting and endpoints.
For a range, we want a start, an end/stop, a step. The start and stop define the beginning and end of the Range, and the step can be as per user requirement.
Start and Stop are both inclusive in Range and If the step is not altered, then by default, the step is 1.
In this blog we will be talking about Kotlin Ranges, how to create them, and also different types of methods like: rangeTo(), downTo(), reversed(), step(). Let us start it with creation of our desired range.
Range Creation
The Range can be created in Kotlin in three ways
With the use of (..) operator
With the use of rangeTo() function
With the use of downTo() function
Range Creation with (..) operator
This operator builds a range from start to end, including the start and end values. It is the rangeTo() function’s operator form.
Note: (..) operator can be used to create a range for both integers and characters.
Let us see the use of this operator using integer and character range.
Code:
fun main() {
println("Example with Integer range:")
for(num in 1..10){ // creating range with start 1 and end 10
println(num)
}
}
Output:
Example with Integer range:
1
2
3
4
5
6
7
8
9
10
Now, similarly, we can create a range with character range(see an example of char range from ‘a’ to ‘k’ given below).
Code:
fun main() {
println("Example with Character range:")
for(ch in 'a'..'k'){ // creating range with start a and end k
print(ch + " ")
}
}
Output:
Example with Character range:
a b c d e f g h i j k
Range creation with rangeTo() function
It is similar to the (..) operator. It will create a range up to the value passed as an argument. It is also used to create Range for integers as well as characters.
It works in the same way as the (..) operator. It will make a range to the value passed as argument in rangeTo(). It can also be used for making integer and character ranges.
Let us see an example of range creation with rangeTo().
Code:
fun main() {
println("rangeTo() with Integer range:")
for(x in 1.rangeTo(9)){
println(x)
}
}
Output:
rangeTo() with Integer range:
1
2
3
4
5
6
7
8
9
Example of using rangeTo() for creation of char type range is given below:
Code:
fun main() {
println("rangeTo() with Character range:")
for(x in 'a'.rangeTo('k')){
println(x)
}
}
Output:
rangeTo() with Character range:
a
b
c
d
e
f
g
h
i
j
k
Range creation with downTo() function
It works in the opposite direction of the rangeTo() or (..) operator. It generates a range in descending order, from larger to smaller values. In the examples below, we generate ranges in reverse order for both integers and characters.
Code:
fun main() {
println("downTo() with Integer range:")
for(x in 10.downTo(5)){
println(x)
}
}
Output:
downTo() with Integer range:
10
9
8
7
6
5
Range creation of char type range with downTo() method is shown below:
Code:
fun main() {
println("downTo() with Character range:")
for(ch in 'k'.downTo('a')){
println(ch)
}
}
Output:
downTo() with Character range:
k
j
i
h
g
f
e
d
c
b
a




