205b
<< <> >>

5. DO-LOOP, CASE

At runtime, the pair DO-LOOP works just like BEGIN-AGAIN with the following extra's:
  1. DO ( boundary start_count -- ) builds a count mechanism.
  2. Inside the loop the count is accessible with I.
  3. LOOP adjusts the count after each iteration.
  4. LOOP falls through when the counter has finished the indicated range.
To formulate it precisely: the counter finishes the indicated range when it passes the "fence post" between 'boundary-minus-1' and 'boundary.' The same goes for positive and negative steps in case of +LOOP.
 10 1 DO ...           ( 1 2 .. 9 ready )
  0 0 DO ...           ( 0 1 .. -2 -1 ready )
 20 0 DO ...  2 +LOOP  ( 0 2 .. 18 ready )
-10 0 DO ... -1 +LOOP  ( 0 -1 .. -10 ready )
  0 0 DO ... -1 +LOOP  ( 0 ready )
The word LEAVE is used to prematurely exit a DO-LOOP:
.. DO .. IF LEAVE THEN .. LOOP aaa
LEAVE removes the counter administration and causes a jump to aaa, immediately after LOOP.

DO-LOOP resembles BEGIN-AGAIN even at compile-time. That's why it is possible to use WHILE to compile a jump out of a DO-LOOP. This has its use when the continuation of the conditionally aborted loop differs substantially from that of the normal loop:
.. DO .. WHILE LOOP aaa ELSE .. I .. UNLOOP THEN ..
aaa is executed when LOOP finishes the loop. The part between ELSE and THEN is used only after the conditional loop exit. In most cases there is no way to do this elegantly using LEAVE instead. Two remarks:
  1. In all cases, the counter administration must be removed explictly using UNLOOP.
  2. I is usable outside the loop, as long as the counter administration is still intact.
LEAVE (always) and LOOP (after the last pass) implicitly execute UNLOOP.

To finish off, Forth's CASE-construct. Hot debates have accompanied CASE ever since it was first proposed. Is it worth all that attention? Some people, including this author, think not. Forth has so many constructs available that CASE is actually redundant. Some people feel that CASE is only there to let Forth beginners feel at home with a construct they might recognize from their previous programming environments (and that this is doing more bad than good).

einde
 >>