«« «» »» |
5. DO-LOOP, CASEAt runtime, the pair DO-LOOP works just like BEGIN-AGAIN with the following extra's:
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 aaaLEAVE 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:
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). |