Loops/For with a specified step: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
Line 2,924:
=={{header|Wren}}==
There is currently no direct way to incorporate a step into a ''for'' loop but we can simulate it by declaring a second variable at the start of the loop which maps the loop variable to the value we want or we can simply use a ''while'' loop instead.
<syntaxhighlight lang="ecmascriptwren">// Print odd numbers under 20.
for (i in 1..10) {
var j = 2*i - 1
Line 2,948:
{{libheader|Wren-iterate}}
A further and more general approach is to use a wrapper class (such as the one in the above module) which can iterate over any sequence in a stepped fashion using Wren's ''iterator protocol''.
<syntaxhighlight lang="ecmascriptwren">import "./iterate" for Stepped
 
// Print odd numbers under 20.
9,482

edits