Loops/For with a specified step: Difference between revisions

m
(→‎{{header|Ruby}}: add sample using named arguments)
Line 1,528:
2 4 6 8 who do we appreciate?</syntaxhighlight>
 
Note that an expression of the form <code>(start, step) (p. i.) count</code> will generate the specified numbers (p. is J's polynomial primitive, i. is J's index generator). So to generate the above sequence of integers we could have used:
Or, using an actual for loop:
 
<syntaxhighlight lang=J> 0 2 (p. i.) 5
0 2 4 6 8</syntaxhighlight>
 
Or, using an "actual" for loop:
 
<syntaxhighlight lang="j"> 3 :0''
Line 1,559 ⟶ 1,564:
1 4 7 10 13 16 19</syntaxhighlight>
 
And, of course, like filtering in any language, this approach supports non-constant step sizes, either by applying a function to each argument individually:
 
<syntaxhighlight lang="j"> (#~ 1&p:) 1 thru 20
2 3 5 7 11 13 17 19</syntaxhighlight>
 
Or, by inserting a combining function between each value:
 
<syntaxhighlight lang="j"> (-&{.,])/ 1 thru 20
_10 11 _9 12 _8 13 _7 14 _6 15 _5 16 _4 17 _3 18 _2 19 _1 20</syntaxhighlight>
 
Other structural approaches can also be viable...
 
=={{header|Java}}==
6,962

edits