
* created by Chaitanya for Beginnersbook. We can reverse a range in Kotlin using reversed() function. However if we want a specific interval like 3 then we can define the range like this 1.10.step(3) this way the values would be 1 4 7 10. By default the value of step is 1 so when we create range 1.10, it is 1, 2, 3.10. With the help of step() function we can define the interval between the values. Similarly we can create character ranges such as ‘A’.’D’ which will create a series of values A, B, C, D. For example a range of 1.5 would create a series of values 1, 2, 3, 4, 5. The value which is equal or greater than start value and smaller or equal to end value comes inside the defined range. With the help of ranges in Kotlin we can easily create a list of sequence by specifying starting and ending value. Range expressions are created with operator (.) which is complemented by in and in. we can use these functions rangeTo() and downTo(), rangeTo() is for increasing order and downTo() is for decreasing order. Kotlin range is defined as an interval from start value to the end value. Println("3 in oneToTen: $")ġ1 in oneToTen: false Kotlin Range: rangeTo() and downTo() functions

Lets see how to do this with the help of a simple example. We can also check whether a particular element is present in the range or not.

We are cycling through the elements of the ranges using for loop. In the following example, we have created two ranges, one is an integer range and other one is a character range. We can also create ranges in reverse order and several other things with ranges. I found out that Kotlin has ranges and I can use them as follows: 1.n But this is an inclusive range which includes 1 and n. For example a range of 1.5 would create a series of values 1, 2, 3, 4, 5. I want to create a range from 1 to n where n is excluded.

With the help of ranges in Kotlin we can easily create a list of sequence by specifying starting and ending value. Package kotlin.ranges /** * A range of values of type `Char`.In this guide, we will discuss the very cool feature of Kotlin which is ranges. Range expressions are created with operator (.) which is complemented by in and.

ClosedRange is further extended by CharRange, IntRange and LongRange which provides the functionality of Iterable also along with closed range which is used by for loops. Kotlin range is defined as an interval from start value to the end value.
