Skipping values with step
By default a range moves up by 1. To move by a different amount, add step after the range:
for (i in 2..10 step 2) {
println(i)
}
This prints the even numbers from 2 through 10:
2
4
6
8
10
Read it from left to right: start at 2, go up to 10, and take steps of 2. The loop visits 2, then 4, 6, 8 and 10, and the next value, 12, is past the end, so it stops.
How the three parts map to the range
- Start: the left end of the range, here 2.
- End: the right end, here 10. Because
..includes its end, 10 is printed since the steps happen to land on it. - Step: the number after
step, here 2. It is the amount added after every pass.
The end does not have to be hit exactly
The range gives an upper limit, not a promised last value. A loop stops at the last value that does not go past the end. With a step that does not land on the end, the end is simply never printed:
for (i in 1..10 step 4) {
println(i)
}
This prints 1, 5 and 9. The next value, 13, is beyond 10. This is why a range is safer than an exact test like "stop when the counter equals 10", which could be jumped over.
Rules for step
- The step must be positive. A step of 0 or a negative number throws an
IllegalArgumentExceptionwhen the loop starts, with the messageStep must be positive, was: -2.(or0). To count downward, usedownTowith a positive step, as the next lessons show. stepbelongs to a range. It follows..,untilordownTo.0 until 10 step 5visits 0 and 5.- Parentheses are optional.
2..10 step 2is a call written in infix form, the same as(2..10).step(2). Using it without parentheses is the usual style.
The exercise: odd numbers from 7 through 23
The task is to print all odd numbers from 7 through 23: 7, 9, 11, and so on up to 23.
- Choose the start and end. The first odd number is 7 and the last is 23, so the range is
7..23. - Choose the step. Consecutive odd numbers differ by 2. Starting at an odd number and adding 2 each time never leaves the odd numbers, so add
step 2. - Print each value. The body is
println(i).
The trick is that the start decides the parity. If the loop began at 8 with the same step, it would print even numbers instead. You do not need an if to test oddness.
The solution in Kotlin
for (i in 7..23 step 2) {
println(i)
}
It prints 7, 9, 11, 13, 15, 17, 19, 21 and 23, one per line. That is nine lines.
Details worth knowing
- Counting the passes. The number of values is the distance divided by the step, rounded down, plus one:
(23 - 7) / 2 + 1is 9. - Same result with
until.7 until 24 step 2visits the same nine values. Both forms are correct. - Filtering is an alternative, not a better one. You could loop over every number and print only those with
i % 2 == 1. It runs twice as many passes.stepsays the intent directly.
Common mistakes
- Wrong first value. Starting at 6 or 8 prints even numbers, as the parity follows the start.
- Using
stepwithout a range.for (i in step 2)does not compile. - Using a step of 1 and forgetting to filter. It prints every number from 7 to 23.
- Expecting the end to be printed. With a step that skips over it, as in
1..10 step 4, the end never appears.
Key takeaways
a..b step kvisitsa,a + k,a + 2kand so on, while the value is at mostb.- The step must be positive.
- The start value decides whether you get even or odd numbers when the step is 2.
- For odd numbers from 7 through 23:
for (i in 7..23 step 2) { println(i) }.