In Kotlin, the range is a collection of finite values defined by endpoints. Kotlin's ranges consist of a beginning, a stop, and a step. The start and stop range are included, and the value of the step is the default1. The range is also used with similar types.
There are three ways to create ranges:
The range used for forEach loop - The forEach loop is used to move through the range.
Using the keyword step, we can enter a step between the values. It is mainly used to fill in the gaps between two values in the RangeTo () or downTo () or in (..) operator. The default value of the step is 1, and the value of the step function should not be 0.
It is used to inverse a given range type. Instead of downTo (), we can use the reverse () function to print the array in descending order.
Pre-defined functions - There are some pre-defined functions in range. They are max (), min (), average () and sum ().
|