Loops/For with a specified step: Difference between revisions

Line 612:
i:8j8
_8 _6 _4 _2 0 2 4 6 8</lang>
 
Or, if we prefer, we could borrow the definition of <code>thru</code> from the [[Loops/Downward_for#J|Downward for]] task and then filter for the desired values:
 
<lang J> thru=: <./ + i.@(+*)@-~</lang>
 
Example use:
 
<lang J> (#~ 0 = 2&|) 1 thru 20
2 4 6 8 10 12 14 16 18 20
(#~ 0 = 3&|) 1 thru 20
3 6 9 12 15 18
(#~ 1 = 3&|) 1 thru 20
1 4 7 10 13 16 19</lang>
 
And, of course, like filtering in any language, this approach supports non-constant step sizes:
 
<lang J> (#~ 1&p:) 1 thru 20
2 3 5 7 11 13 17 19</lang>
 
=={{header|Java}}==
6,962

edits