Iterate from 0 to 20 with a step of 5 using for-range with by.
← Control Flow · Ref: Q1149
'by' specifies the step value, always positive:
for i in 0 ... 20 by 5 stdout.println($i)
EK9 determines direction from start vs end. Descending: for i in 20 ... 0 by 5. See Q1148.
Example
defines module qa.controlflow.forrangewithby defines program ForRangeWithByDemo() stdout <- Stdout() //Step by 5 stdout.println("By 5:") for i in 0 ... 20 by 5 stdout.println($i)
Common mistakes
E50001 — EK9 uses 'for i in start ... end by step'. No range() function.
Incorrect:
for i in range(0, 20, 5)
Correct:
for i in 0 ... 20 by 5
Other ways to ask this
- I need a loop that counts in steps of 5 instead of 1
- In Python I'd use range(0, 21, 5). Write the EK9 range with step
- Given start=0, end=20, step=5, iterate with a custom increment
- Loop through a range with a specific step value using 'by'
Coming from another language?
Python: range(0, 21, 5). Java: for(int i=0; i<=20; i+=5). Go: for i := 0; i <= 20; i += 5. EK9: for i in 0 ... 20 by 5.
Keywords: by, step, range, for, custom, increment