With the for loop, you can also create ranges of values. A range is a collection of finite values defined by a start, an end, and an interval.
You can create ranges using the .. operator.
for (nums in 1..5) {
println(nums) // Prints 1, 2, 3, 4, 5
}
If you want to create a range that goes backward, use the downTo keyword. If you want to skip values, use the step keyword.
for (nums in 10 downTo 1 step 2) {
println(nums) // Prints 10, 8, 6, 4, 2
}
untilIf you want to create a range that excludes the last value, use the until keyword instead of ...
for (i in 1 until 5) will print 1, 2, 3, 4.