Loop structures: Difference between revisions

Content added Content deleted
Line 267: Line 267:
==[[Forth]]==
==[[Forth]]==
===DO-LOOP===
===DO-LOOP===
<syntaxhighlight lang="forth">
( limit start ) DO ( iterated statements ) LOOP
( limit start ) DO ( iterated statements ) ( increment ) +LOOP
( limit start ) DO ( iterated statements ) LOOP
( limit start ) DO ( iterated statements ) ( increment ) +LOOP
LEAVE \ exits a DO loop
LEAVE \ exits a DO loop
UNLOOP EXIT \ cleans up loop counters from return stack before returning from the current word
UNLOOP EXIT \ cleans up loop counters from return stack before returning from the current word
</syntaxhighlight>

example: Two standard iterations
example: Two standard iterations
<syntaxhighlight lang="forth">
10 0 DO I . LOOP \ Prints the numbers from 0 to 9
10 0 DO I . 2 +LOOP \ Prints the even numbers from 0 to 8
10 0 DO I . LOOP \ Prints the numbers from 0 to 9
10 0 DO I . 2 +LOOP \ Prints the even numbers from 0 to 8
</syntaxhighlight>


===BEGIN-UNTIL===
===BEGIN-UNTIL===
<syntaxhighlight lang="forth">
BEGIN ( iterated statements ) ( conditional ) UNTIL
BEGIN ( iterated statements ) ( conditional ) UNTIL
</syntaxhighlight>

example: Counts down from a given number to zero
example: Counts down from a given number to zero
<syntaxhighlight lang="forth">
: COUNTDOWN ( n -- ) BEGIN DUP CR . 1- DUP 0< UNTIL DROP ;
: COUNTDOWN ( n -- ) BEGIN DUP CR . 1- DUP 0< UNTIL DROP ;
</syntaxhighlight>


===BEGIN-AGAIN===
===BEGIN-AGAIN===
<syntaxhighlight lang="forth">
BEGIN ( iterated statements ) AGAIN
BEGIN ( iterated statements ) AGAIN
</syntaxhighlight>

example: echo user's input
example: echo user's input
<syntaxhighlight lang="forth">
: FOREVER ( -- ) BEGIN KEY EMIT AGAIN ;
: FOREVER ( -- ) BEGIN KEY EMIT AGAIN ;
</syntaxhighlight>


===BEGIN-WHILE-REPEAT===
===BEGIN-WHILE-REPEAT===
<syntaxhighlight lang="forth">
BEGIN ( unconditional iterated statements ) ( conditional ) WHILE ( conditional iterated statements ) REPEAT
BEGIN ( unconditional iterated statements ) ( conditional ) WHILE ( conditional iterated statements ) REPEAT
example: counts down from a given number to one
example: counts down from a given number to one
: COUNTDOWN ( n -- ) BEGIN DUP WHILE CR DUP . 1- REPEAT DROP ;
: COUNTDOWN ( n -- ) BEGIN DUP WHILE CR DUP . 1- REPEAT DROP ;
</syntaxhighlight>
Additional WHILE clauses may be added to a loop, but each extra WHILE requires a matching THEN after the REPEAT.
Additional WHILE clauses may be added to a loop, but each extra WHILE requires a matching THEN after the REPEAT.


Line 295: Line 312:


A good example of a useful combination is this complex loop:
A good example of a useful combination is this complex loop:
<syntaxhighlight lang="forth">
BEGIN
BEGIN
( condition 1 )
( condition 1 )
WHILE
WHILE
( condition 2 )
( condition 2 )
UNTIL
UNTIL
( condition 2 succeeded )
( condition 2 succeeded )
ELSE
ELSE
( condition 1 failed )
( condition 1 failed )
THEN
THEN
An example of using this idiom in practice might be this pseudo-Forth
</syntaxhighlight>
BEGIN
read-next-record
WHILE
found-record
UNTIL
process-record
ELSE
error" Ran out of records looking for the right one!"
THEN


An example of using this idiom in practice might be this pseudo-Forth
<syntaxhighlight lang="forth">
BEGIN
read-next-record
WHILE
found-record
UNTIL
process-record
ELSE
error" Ran out of records looking for the right one!"
THEN
</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==