Kotlin Ranges

29 Nov, 2019
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
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 –
fun main(args : Array<String>){      println("Integer range:")    // creating integer range     for(num in 1..5){        println(num)    }}
Output:
Integer range:
1
2
3
4
5

Kotlin program of character range using (..) operator –
fun main(args : Array<String>){      println("Character range:")    // creating character range     for(ch in 'a'..'e'){        println(ch)    }}
Output:
Character range:
a
b
c
d
e

It is similar to (..) operator. 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 –
fun main(args : Array<String>){      println("Integer range:")    // creating integer range     for(num in 1.rangeTo(5)){        println(num)    }}
Output:
Integer range:
1
2
3
4
5

Kotlin program of character range using rangeTo() function –
fun main(args : Array<String>){      println("Character  range:")    // creating character range    for(ch in 'a'.rangeTo('e')){        println(ch)    }}
Output:
Character  range:
a
b
c
d
e

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 –
fun main(args : Array<String>){      println("Integer range in descending order:")    // creating integer range    for(num in 5.downTo(1)){        println(num)    }}
Output:
Integer range in descending order:
5
4
3
2
1

Kotlin program of character range using downTo() function –
fun main(args : Array<String>){      println("Character range in reverse order:")    // creating character range    for(ch in 'e'.downTo('a')){        println(ch)    }}
Output:
Character range in reverse order:
e
d
c
b
a

The forEach loop is also used to traverse over the range.
fun main(args : Array<String>){      println("Integer  range:")    // creating integer range    (2..5).forEach(::println)}
Output:
Integer  range:
2
3
4
5

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 –
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)  }
Output:
3 5 7 9 
11
19
5

It is used to reverse the given range type. Instead of downTo() we can use reverse() function to print the range in descending order.
fun main(args: Array<String>) {    var range = 2..8    for (x in range.reversed()){        print("$x ")    }}
Output:
8 7 6 5 4 3 2 
There are some predefined function in Kotlin Range: min(), max(), sum(), average().
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())}
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

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")}
Output:
2 does not lie within the range
shubham_singh
Kotlin Ranges
Picked
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.