Kotlin Ranges
Ranges are used to express a series of number with a starting value and an ending value.
Kotlin Ranges could be useful in expression evaluation and looping statements. Usually in looping statements, a variable is created initially and is incremented/decremented through repetitions, and an expression is checked if the value is greater that or less than some limit value. Instead of this boiler code, Kotlin Ranges could be used to repeat a set of statements, starting with a value on lower or upper limit and progressing towards upper or lower limit respectively.
Kotlin Ranges could be found in three forms.
By default, the step value from m to n is 1. You can change this step value by specifying a step value after the Kotlin Range using step
keyword. We shall look into these examples as well.
In this tutorial we will learn about different forms of Kotlin Ranges and how to use them in For Loop statement and If Conditional statement.
Kotlin Range – m..n
m..n corresponds to the range [m,n] given m<n. By default, m and n are included in the range.
In the following example, we shall look into the usage of Kotlin range m..n in Loop statement.
Kotlin Program – example.kt
fun main(args: Array<String>) {
for(i in 2..4){
println(i)
}
}
Run the program. For loop iterates with i
starting from 2 to 4, both included.
Output
2
3
4
The step value by default for for-in-range is 1. But, you can change this step value using step keyword. step keyword transforms the range into a progresstion.
In the following example, we shall use step keyword and run the for loop in steps of 3 in the given range.
Kotlin Program – example.kt
/**
* Kotlin Range
*/
fun main(args: Array<String>) {
for(i in 0..10 step 3){
println(i)
}
}
Run the program. For loop iterates with i
starting from 2 to 4, both included.
Output
0
3
6
9
You can also use Kotlin range m..n in a conditional statement like if-statement. Using in keyword, you can check if a value is in the given range.
In the following example, we shall check if the number 3
is present in given range 2..4
.
Kotlin Program – example.kt
fun main(args: Array<String>) {
var r = 3
if(r in 2..4){
println(r.toString() + " is in the range 2..4")
}
}
Run the above Kotlin program.
Output
3 is in the range 2..4
We have used a range, if-in statement, to check if a given item is present in the range.
Kotlin Range – n downTo m
n downTo m corresponds to the range [n,m] given m<n. By default, m and n are included in the range.
In the following example, we shall use the Kotlin range n downTo m in Loop statement
Kotlin Program – example.kt
fun main(args: Array<String>) {
for(i in 4 downTo 2){
println(i)
}
}
Run the above example Kotlin program.
Output
4
3
2
Now, let us use this Kotlin range n downTo m in a conditional statement to check if an element is present in the range.
Kotlin Program – example.kt
fun main(args: Array<String>) {
var r = 3
if(r in 4 downTo 1){
println(r.toString() + " is in the range")
}
}
Run the above Kotlin program.
Output
3 is in the range
3 is of course present in the range 4 downTo 1
. Therefore, r in 4 downTo 1
returns true.
Kotlin Range – m until n
m until n corresponds to the range [m,n) given m<n. By default, m is included in the range and n is excluded from the range.
In the following example, we shall use Kotlin range in the form m until n in for loop statement.
Kotlin Program – example.kt
fun main(args: Array<String>) {
for(i in 2 until 4){
println(i)
}
}
Run the above example program. In the range, 2 until 4, we have only 2, 3. 4 is not included in the range.
Output
2
3
In the following example, we use Kotlin range m until n in if conditional statement.
Kotlin Program – example.kt
fun main(args: Array<String>) {
var r = 3
if(r in 1 until 4){
println(r.toString() + " is in the range")
}
}
Run the above example program.
Output
3 is in the range
Conclusion
In this Kotlin Tutorial, we learned about the syntax and usages of ranges in Kotlin.