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.

  1.  m..n
  2.  n downTo m
  3.  m until n

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.

Run the program. For loop iterates with i starting from 2 to 4, both included.

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.

Run the program. For loop iterates with i starting from 2 to 4, both included.

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.

Run the above Kotlin program.

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 rangen downTo m  in Loop statement

Run the above example Kotlin program.

Now, let us use this Kotlin rangen downTo m  in a conditional statement to check if an element is present in the range.

Run the above Kotlin program.

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.

Run the above example program. In the range, 2 until 4, we have only 2, 3. 4 is not included in the range.

In the following example, we use Kotlin rangem until n  in if conditional statement.

Run the above example program.

Conclusion

In this Kotlin Tutorial, we learned about the syntax and usages of ranges in Kotlin.