Kotlin Ranges

In Kotlin, the range is a collection of finite values which is defined by endpoints. The range in Kotlin consists of a start, a stop, and the step. The start and stop are inclusive in the Range and the value of step is by default 1.
The range is used with comparable types.

There are three ways for creating Range in Kotlin –

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

(..) operator

It is the simplest way to work with range. It will create a range from the start to end including both the values of start and end. It is the operator form of rangeTo() function. Using (..) operator we can create range for integers as well as characters.
Kotlin program of integer range using (..) operator –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>){
  
    println("Integer range:")
    // creating integer range 
    for(num in 1..5){
        println(num)
    }
}

chevron_right


Output:

Integer range:
1
2
3
4
5

Kotlin program of character range using (..) operator –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>){
  
    println("Character range:")
    // creating character range 
    for(ch in 'a'..'e'){
        println(ch)
    }
}

chevron_right


Output:

Character range:
a
b
c
d
e

rangeTo() function

It is similar to (..) opeartor. It will create a range upto the value passed as an argument. It is also used to create range for integers as well as characters.
Kotlin program of integer range using rangeTo() function –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>){
  
    println("Integer range:")
    // creating integer range 
    for(num in 1.rangeTo(5)){
        println(num)
    }
}

chevron_right


Output:

Integer range:
1
2
3
4
5

Kotlin program of character range using rangeTo() function –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>){
  
    println("Character  range:")
    // creating character range
    for(ch in 'a'.rangeTo('e')){
        println(ch)
    }
}

chevron_right


Output:

Character  range:
a
b
c
d
e

downTo() function

It is reverse of the rangeTo() or (..) operator. It creates a range in descending order, i.e. from bigger values to smaller value. Below we create range in reverse order for integer and characters both.
Kotlin program of integer range using downTo() function –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>){
  
    println("Integer range in descending order:")
    // creating integer range
    for(num in 5.downTo(1)){
        println(num)
    }
}

chevron_right


Output:

Integer range in descending order:
5
4
3
2
1

Kotlin program of character range using downTo() function –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>){
  
    println("Character range in reverse order:")
    // creating character range
    for(ch in 'e'.downTo('a')){
        println(ch)
    }
}

chevron_right


Output:

Character range in reverse order:
e
d
c
b
a

Range using forEach loop –

The forEach loop is also used to traverse over the range.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>){
  
    println("Integer  range:")
    // creating integer range
    (2..5).forEach(::println)
}

chevron_right


Output:

Integer  range:
2
3
4
5

step()

With keyword step, one can provide step between values. It is mainly used in order to provide the gap between the two values in rangeTo() or downTo() or in (..) operator. The default value for step is 1 and the value of step function cannot be 0.

Kotlin program of using step –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun  main(args: Array<String>) {
    //for iterating over the range
    var i = 2
    // for loop with step keyword
    for (i in 3..10 step 2
        print("$i "
    println()
    // print first value of the range
    println((11..20 step 2).first) 
    // print last value of the range
    println((11..20 step 4).last)  
    // print the step used in the range
    println((11..20 step 5).step)  
}

chevron_right


Output:

3 5 7 9 
11
19
5

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var range = 2..8
    for (x in range.reversed()){
        print("$x ")
    }
}

chevron_right


Output:

8 7 6 5 4 3 2 

Some predefined function in range –

There are some predefined function in Kotlin Range: min(), max(), sum(), average().

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main() {
  
    val predefined = (15..20)
  
    println("The minimum value of range is: "+predefined.min())
    println("The maximum value of range is: "+predefined.max())
    println("The sum of all values of range is: "+predefined.sum())
    println("The average value of range is: "+predefined.average())
}

chevron_right


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

Check whether a value lies within a range or not?

filter_none

edit
close

play_arrow

link
brightness_4
code

fun  main(args: Array<String>)
{
    var i = 2
    //to check whether the value lies in the range
    if( i in 5..10)
        println("$i is lie within the range")
    else
        println("$i does not lie within the range")
}

chevron_right


Output:

2 does not lie within the range


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.