Updated April 20, 2023
Introduction to Kotlin Range
In kotlin, the range is defined as the interval from the start value to end value. It is mostly used to print the number with a specific range. In kotlin, using range we can easily print the numbers which we want within a specific range, it reduces the number of lines and makes the code more reliable. It considers the value which is equal or greater than the start value and smaller or equal to the end value. Here in this article, we are going to discuss this range function and its usage.
Syntax:
In kotlin, we can define the range using operator (..) which is complemented by in such as
for(variable in start value..end value)
Here start value defines the first value of the range and the end value defines the last number in the range.
How to use range in kotlin?
In this section, we are going to discussed using range in kotlin using various ways.
Example #1
Here we have written a program to print the number from 1 to 10 using a range.
Code:
fun main(args: Array<String>)
{
println("Print number from 1 to 10")
for(x in 1..10)
{
println(x)
}
}
Output:
Here we have used x variable to store the number, as we want to print the number from 1 to 10, we have defined the start value as 1 and the end value as 10.
Example #2
Here we have written a program to print the alphabets from a to k using range
Code:
fun main(args: Array<String>)
{
println("Print alphabet from a to k")
for(x in 'a'..'k')
{
println(x)
}
}
Output:
Here we have used x variable to store the alphabets, as we want to print the alphabet from a to k, we have defined start value as a and end value ask.
Example #3
There are some cases where we want to print numbers or alphabets in a reversed manner. In the below examples, we have tried to print the number and alphabets in a reversed manner using the same method as we have used in examples 1 and 2, but the same concept doesn’t work as the start value must be small than the end value. If we try to do this, it will runs the program but will not print anything.
Code:
fun main(args: Array<String>)
{
println("Print numbers from 10 to 1")
for(x in 10..1)
{
println(x)
}
}
Output:
As the start value is greater than the end value, the output will not print anything
Code:
fun main(args: Array<String>)
{
println("Print alphabet from k to a")
for(x in 'k'..'a')
{
println(x)
}
}
Output:
As the start value is greater than the end value, the output will not print anything
Use of downTo method
As we can see in example 3, there is nothing in output. To solve the above problem downTo() method is used to print the number or alphabets in a reversed manner.
Example #1
Program to print numbers from 10 to 1 using downTo method
Code:
fun main(args: Array<String>)
{
println("Print numbers from 10 to 1")
for(x in 10 downTo 1)
{
println(x)
}
}
Output:
Example #2
Program to print alphabets from k to a using downTo method
Code:
fun main(args: Array<String>)
{
println("Print alphabet from k to a")
for(x in 'k' downTo 'a')
{
println(x)
}
}
Output:
Use of until method
Until a method is used to print the values within a specific range excluding the last element. It iterates range from the start value to 1 less than the end.
Example #1
Program to print numbers from 1 to 9 using until method
Code:
fun main(args: Array<String>)
{
println("Print numbers from 1 to 9")
for(x in 1 until 10)
{
println(x)
}
}
Output:
Here we have written a program to print the numbers from 1 to 9 using until method. As we want to print numbers from 1 to 9 and until the method excludes the last element we have defined the start value as 1 and the end value as 10
Use of rangeTo method
rangeTo method is used to print the values from the start value to end value in increasing order. Here we have written a program to print numbers from 0 to 7 as follows.
Code:
fun main(args: Array<String>)
{
println("Print numbers from 0 to 7")
for(x in 0.rangeTo(7))
{
println(x)
}
}
Output:
Use of step method
The step method is used to return the range value in an interval of given step value.
Example #1
Code:
fun main(args: Array<String>)
{
println("Print numbers using step 2")
for(x in 1..20 step 2)
{
println(x)
}
println("Print numbers using step 5")
for(x in 1..20 step 5)
{
println(x)
}
}
Output:
Here we have written a program to print the numbers using step 2 value for 1 to 20 range and step 5 value for 1 to 20 range. In the output, you can see that in the first half the numbers are printed using 2 intervals,s and in the second half the interval the numbers are print using 5 intervals.
Example #2
Here we have written a program to print the number in a reversed manner using an interval.
Code:
fun main(args: Array<String>)
{
println("Print numbers using step 2")
for(x in 20 downTo 1 step 2)
{
println(x)
}
println("Print numbers using step 5")
for(x in 20 downTo 1 step 5)
{
println(x)
}
}
Output:
Use of reversed method
reversed method is used to print values in reversed order of the given range. Here we have written a program to print the numbers from 1 to 7 in reversed order.
Code:
fun main(args: Array<String>)
{
var y = 1..7
println("Print numbers using reversed")
for(x in y.reversed())
{
println(x)
}
}
Output:
Conclusion
Here we have discussed range operators using examples. We have also discussed various range methods downTo, until, step, and reversed with examples.
Recommended Articles
This is a guide to Kotlin Range. Here we discuss the introduction and how to use range in kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –