Loop structures: Difference between revisions
→Forth
(→Factor) |
(→Forth) |
||
Line 267:
==[[Forth]]==
===DO-LOOP===
<syntaxhighlight lang="forth">
( limit start ) DO ( iterated statements ) LOOP▼
</syntaxhighlight>
example: Two standard iterations
<syntaxhighlight lang="forth">
10 0 DO I . LOOP \ Prints the numbers from 0 to 9▼
</syntaxhighlight>
===BEGIN-UNTIL===
<syntaxhighlight lang="forth">
</syntaxhighlight>
example: Counts down from a given number to zero
<syntaxhighlight lang="forth">
</syntaxhighlight>
===BEGIN-AGAIN===
<syntaxhighlight lang="forth">
</syntaxhighlight>
example: echo user's input
<syntaxhighlight lang="forth">
</syntaxhighlight>
===BEGIN-WHILE-REPEAT===
<syntaxhighlight lang="forth">
example: counts down from a given number to one
</syntaxhighlight>
Additional WHILE clauses may be added to a loop, but each extra WHILE requires a matching THEN after the REPEAT.
Line 295 ⟶ 312:
A good example of a useful combination is this complex loop:
<syntaxhighlight lang="forth">
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">
</syntaxhighlight>
=={{header|FreeBASIC}}==
|