Signup/Sign In

Kotlin Range

In this tutorial, we will discuss about range in Kotlin. In the mathematical sense, a range is a sequence of numbers, characters, etc. within a defined boundary. The boundary points are start & end and both of them are included in the range. The range may also have a step (step is used to jump numbers or characters while defining the range from start to end boundaries).

We can create a range with the help of rangeTo() function in Kotlin or using the .. operator.

Kotlin: Create a range

As we mentioned above, we can either use the .. operator and the rangeTo() function, let's cover them one by one.

Kotlin Range using the .. Operator:

Let us create a range from 1 to 9 using the .. operator.

val range = 1..9

In the code example above, we created a range using the .. operator. If you want to create a range of numbers with all the numbers included in the range then using the .. operator is easy and simple.

The only shortcoming is that we cannot create a range with a pattern where we skip/jump numbers, for example, if you want to create a range of number 1, 3, 5, 7, etc. jumping one number, or 1, 4, 7, 10, etc. jumping two numbers.

Kotlin Range using the rangeTo() Operator:

We can also use rangeTo() function to create the range. Here is a basic example of it:

val range = 1.rangeTo(9)

In the code example above we are creating a range from 1 to 9, including both 1 and 9 in it.

Check if Number is in a given Range

After you have created a range variable, if you want to check if a given number falls in that range or not, then we can do it easily using the in operator with if condition.

Here is a simple code example:

fun main() {
    val range = 1..9
    if (1 in range)
        println("1 is in $range")
    if(10 in range)
        println("10 is in $range")
}


1 is in 1..9

We can also use a range in various different scenarios:

var lowerCase = 'a'..'z'          // Range for lowercase alphabet
var upperCase = 'A'..'Z'          // Range for uppercase alphabet
var digit = 0..9                 // Range for digits

This way we can easily define range of different types as per our requirements.

Kotlin Step in Range

To define distance between two consecutive values in a range, we can use the step() function. For example, to print numbers from 1 to 10 with step of 2, we can write the range as:

val range = 1..10 step 2

Let us print this range using for loop:

fun main() {
    val range = 1..10 step 2
    for (number in range)
        println(number)
}


1
3
5
7
9

Similarly, we can provide any number as argument to the step function and based on that our range variable will be created.

Kotlin downTo() function

If we want to print numbers in reverse order i.e. we from 10 to 1, we cannot use rangeTo() function or the .. operator. If we try to use them like in the code below, it does not print anything as default step is 1 and it will proceed in forward direction only.

fun main() {
    val range = 10..1
    for (number in range)
        println(number)     // print nothing
}

So to create a range in the reverse order, we will use the downTo() function:

fun main() {
    val range = 10 downTo 1
    for (number in range)
        println(number)
}


10
9
8
7
6
5
4
3
2
1

We can also use step with downTo() function:

fun main() {
    val range = 10 downTo 1 step 3
    for (number in range)
        println(number)
}


10
7
4
1

Summary

In this tutorial we discussed about rangeTo(), step and downTo() functions. There are also many other functions available like first(), last(), until() which can be used with the range. We will discuss about continue, break and repeat statement next.



About the author:
I'm a writer at studytonight.com.