Loops/Increment loop index within loop body: Difference between revisions
Content added Content deleted
(→{{header|Quackery}}: added second solution) |
m (→With indexed loop word: Change of heart re behaviours of incr and end) |
||
Line 4,166: | Line 4,166: | ||
[ f.index share ] is index ( --> n ) |
[ f.index share ] is index ( --> n ) |
||
[ |
[ f.incr replace ] is incr ( n --> ) |
||
[ |
[ f.end replace ] is end ( b --> ) |
||
[ 1 false ]'[ |
[ 1 false ]'[ |
||
Line 4,186: | Line 4,186: | ||
<code>index</code> returns the current index. |
<code>index</code> returns the current index. |
||
The default increment is <code>1</code>, but this can be overridden for a single iteration by the word <code>incr</code> within the nest being performed by <code>from</code>. <code>incr</code> takes a number from the stack, and sets the |
The default increment is <code>1</code>, but this can be overridden for a single iteration by the word <code>incr</code> within the nest being performed by <code>from</code>. <code>incr</code> takes a number from the stack, and sets the increment to that number for the current iteration. If <code>incr</code> is performed within some iterations but not all, the increment will be <code>1</code> for those iterations where it is not performed. |
||
The task states "''in addition to the normal incrementation''" but this is counter-intuitive. To make the loop task compliant you will need to precede <code>f.incr</code> with <code>1+</code> in <code>incr</code>. You will also need to delete the <code>1+</code> before <code>incr</code> in the task code below. |
|||
⚫ | |||
⚫ | |||
As with other iterative looping words in Quackery (e.g. <code>times</code>, <code>witheach</code>, etc.) the word <code>done</code> will terminate the current iteration immediately. |
As with other iterative looping words in Quackery (e.g. <code>times</code>, <code>witheach</code>, etc.) the word <code>done</code> will terminate the current iteration immediately. |
||
Line 4,212: | Line 4,214: | ||
say ": " |
say ": " |
||
index echo, cr |
index echo, cr |
||
index incr ] |
index 1+ incr ] |
||
dup 42 = |
dup 42 = end ] |
||
drop</syntaxhighlight> |
drop</syntaxhighlight> |
||