Loops/Increment loop index within loop body: Difference between revisions

Content added Content deleted
m (→‎With indexed loop word: tweaked text and code layout)
m (→‎With indexed loop word: reverted behaviour of end.)
Line 4,168: Line 4,168:
[ f.incr replace ] is incr ( n --> )
[ f.incr replace ] is incr ( n --> )


[ f.end replace ] is end ( b --> )
[ true f.end replace ] is end ( b --> )


[ 1 false ]'[
[ 1 false ]'[
Line 4,179: Line 4,179:
f.stacks witheach release ] is from ( n --> )</syntaxhighlight>
f.stacks witheach release ] is from ( n --> )</syntaxhighlight>


<code>from</code> takes its initial index from the stack, and performs the nest that follows it repeatedly until the ending condition is satisfied, incrementing the index at the end of each iteration.
<code>from</code> takes its initial index from the stack, and performs the nest that follows it repeatedly until the ending condition is set to <code>true</code>, incrementing the index at the end of each iteration.


<code>index</code> returns the current index.
<code>index</code> returns the current index.
Line 4,187: Line 4,187:
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.
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.


The word <code>end</code> sets the ending condition to the boolean it takes from the stack, so the loop will end at the end of the current iteration if the boolean is <code>true</code>.
The word <code>end</code> sets the ending condition to <code>true</code>, so the loop will end at the end of the current iteration.


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,212:
index echo, cr
index echo, cr
index 1+ incr ]
index 1+ incr ]
dup 42 = end ]
dup 42 = if end ]
drop</syntaxhighlight>
drop</syntaxhighlight>