Ranges in Kotlin

In this tutorial, we will learn about Ranges in Kotlin. This is a very amazing feature provided by Kotlin with the help of ranges we can create a collection of values by just defining starting and ending value. For example, you want to create a series of 1 to 10 this can be easily done with the help of ranges. There are a total of three ways to create a range in kotlin:

  • Using (..) operator
  • Using rangeTo() function
  • Using downTo() function

Create Ranges in Kotlin Using (..) operator:

It is really easy to create range using (..) operator in Kotlin. You just have to mention the starting and ending point(value) around the (..) operator. Check out the following example program to understand the use of (..) operator to create ranges in kotlin.

Example Program:

fun main(args : Array<String>){ 
  
    println("1 to 5:") 
    // creating range in kotlin using (..) operator
    for(num in 1..5){ 
        println(num) 
    }
}

Output:

1 to 5:
1
2
3
4
5

Create Range in Kotlin rangeTo() function:

Here we will check out another way to create a range in Kotlin. The rangeTo() function is also used for creating ranges in Kotlin. This rangeTo() function can be used to create both character and integer ranges in kotlin. Please check out the following example program to understand rangeTo() function.

Example Program:

fun main(args : Array<String>){ 
  
    println("A to Z:") 
    // creating range in kotlin using rangeTo() function
    for(alpha in 'A'.rangeTo('Z')){ 
        println(alpha) 
    } 
} 

Output:

A to Z:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

Create Range in Kotlin Using downTo() function:

The downTo() function in kotlin used to create ranges in Kotlin in reverse order. For example, you want to create a range from 10 to 1. Please check out the following example program to understand it.

Example Program:

fun main(args : Array<String>){ 
  
    println("Descending order range:") 
    // creating range in kotlin using downTo() function 
    for(num in 10.downTo(1)){ 
        println(num) 
    } 
} 

Output:

Descending order range:
10
9
8
7
6
5
4
3
2
1

Kotlin Range Step() function:

Till now we have learned about three different methods to create ranges in Kotlin and if you learned carefully you can clearly notice that all the functions created ranges with a step size of one. For example in the case of 1 to 10 range was like this 1 2 3 4 5 6 7 8 9 10. But there are many situations when we need to create a range with different step size for example 5 10 15 20 25 30. In this kind of situation, we can use step() function with the range to mention the size of the interval between to values of the range. To understand it please check out the following example program.

Example Program:

fun main(args : Array<String>){

    val myRange = 5..50
    println("5 to 50 with step size 5")
    for(num in myRange.step(5)){
        println(num)
    }
}

Output:

5 to 50 with step size 5
5
10
15
20
25
30
35
40
45
50

Kotlin range reversed() function:

The reversed() function used to create reverse order of the range. Check out the following example program to understand it.

fun main(args : Array<String>){

    val myRange = 1..5
    println("5 to 1")
    for(num in myRange.reversed()){
        println(num)
    }
}

Output:

5 to 1
5
4
3
2
1

 

Spread the love
Scroll to Top