
Here we discuss the introduction and how to use range in kotlin along with different examples and code implementation. We have also discussed various range methods downTo, until, step, and reversed with examples. Here we have discussed range operators using examples. Here we have written a program to print the numbers from 1 to 7 in reversed order. Reversed method is used to print values in reversed order of the given range. Here we have written a program to print the number in a reversed manner using an interval.

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.

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. The step method is used to return the range value in an interval of given step value. Here we have written a program to print numbers from 0 to 7 as follows. RangeTo method is used to print the values from the start value to end value in increasing order.

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 Here we have written a program to print the numbers from 1 to 9 using until method. Program to print numbers from 1 to 9 using until method It iterates range from the start value to 1 less than the end. Until a method is used to print the values within a specific range excluding the last element. Program to print alphabets from k to a using downTo method Program to print numbers from 10 to 1 using downTo method To solve the above problem downTo() method is used to print the number or alphabets in a reversed manner. If we try to do this, it will runs the program but will not print anything.Īs the start value is greater than the end value, the output will not print anythingĪs the start value is greater than the end value, the output will not print anything Use of downTo methodĪs we can see in example 3, there is nothing in output. 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. There are some cases where we want to print numbers or alphabets in a reversed manner. 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. Here we have written a program to print the alphabets from a to k using range

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.
