Loops/For with a specified step: Difference between revisions
Content added Content deleted
No edit summary |
m (→{{header|Wren}}: Minor tidy) |
||
Line 2,924: | Line 2,924: | ||
=={{header|Wren}}== |
=={{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. |
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=" |
<syntaxhighlight lang="wren">// Print odd numbers under 20. |
||
for (i in 1..10) { |
for (i in 1..10) { |
||
var j = 2*i - 1 |
var j = 2*i - 1 |
||
Line 2,948: | Line 2,948: | ||
{{libheader|Wren-iterate}} |
{{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''. |
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=" |
<syntaxhighlight lang="wren">import "./iterate" for Stepped |
||
// Print odd numbers under 20. |
// Print odd numbers under 20. |