       ͻ
        Lesson 5 Part 060  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

         Ŀ
           A Look at the Varieties of DO ... LOOPS 
         

The phrase step 1, step 2, and step 3, represent a sequence of
Forth words.  limit, init, and n are single numbers placed on
the parameter stack.

   ... step 1  limit init   DO  step 2      LOOP  step 3 ...
   ... step 1  limit init   DO  step 2  n  +LOOP  step 3 ...
   ... step 1  limit init  ?DO  step 2      LOOP  step 3 ...
   ... step 1  limit init  ?DO  step 2  n  +LOOP  step 3 ...

Step 1: is executed once before entering each of the loops

limit : is the maximum value of the loop counter.
init  : is the initial value of the loop counter.

DO    : saves the maximum value of the loop counter and the
        current or initial value ( usually on the return stack )
        and continues execution with step 2.
?DO   : first check to see if limit is equal to init and if so
        jumps directly to step 3. If limit and init are not equal
        the action is the same as DO.

step 2: is executed once on each pass of the loop.

LOOP  : increments the loop counter by 1 and then compares the new
        value of the loop counter with limit. If new loop counter
        is equal to the loop limit the loop terminates and execution
        continues with step 3. If the new loop counter is not equal
        to the loop limit then the loop is repeated by performing
        step 2 again.
+LOOP : Increment the current value of the loop counter by the top
        number on the data stack, n, ( instead of 1 ) and then perform
        the same action as LOOP.

step 3: is executed once after completion of the loop.

Note:  In Forth 83  the initial value of the loop counter, the loop
limit and the current value of the loop counter all all unsigned
16 bit integers.

ķ
 Problem 5.9  
Ľ
Given the following loop testing words:
:  DOLOOP   DO  CR I . KEY? ?LEAVE     LOOP ; \ All of these words take
:  DO+LOOP  DO  CR I . KEY? ?LEAVE  2 +LOOP ; \ the loop limit and the
:  DO-LOOP  DO  CR I . KEY? ?LEAVE -2 +LOOP ; \ initial value on the
: ?DOLOOP  ?DO  CR I . KEY? ?LEAVE     LOOP ; \ stack. ie ( l i   -- )
: ?DO+LOOP ?DO  CR I . KEY? ?LEAVE  2 +LOOP ; \ KEY? ?LEAVE allows any
: ?DO-LOOP ?DO  CR I . KEY? ?LEAVE -2 +LOOP ; \ key press to abort.
Determine the output for the following stack inputs.
 a) 10  8   b)  10 10   c)  10  12
 d)  2 -2   e)  -2  2   f)  -1  -3    g) -3  -1
Caution:  Think first!  Some may execute a long long time!!

                Ŀ
                 Leaving Loops Early. 
                

  step 1  limit init   DO    step 2
                             (condition)
                             IF  step 3 LEAVE THEN
                             step 4
                       LOOP  step 5 ...

Execute step 1. Repeat loop as before executing step 2 and step 4 on
each pass - except that if condition is true before loop is finished
execute step 3 and leave loop to execute step 5.  Note:  step 4 will
not be executed if we leave the loop early.
Note:  EXIT cannot be used in DO LOOPs

This is an alternative form if step 3 is not required.
  (condition) could be KEY? or any word that returns a flag.
  step 1  limit init   DO    step 2
                             (condition) ?LEAVE
                             step 4
                       LOOP  step 5

ķ
 Problem 5.10  
Ľ
Consider the following array manipulating words.  RND is your favorite
random number generator   RND ( n -- r )  where  0 <= r < n

CREATE TABLE 100 ALLOT  \ this is a byte array!
: FILL-TABLE  ( -- ) \ Fill table with random numbers.
       100 0 DO 100 RND TABLE I + C! LOOP ;
: SEE-TABLE   ( -- ) \ Display numbers in the table.
       CR 100 0 DO    TABLE I + C@ 4 .R LOOP ;
\ Example of leaving loop early.
: MATCH  ( n   -- )  \ Search and report on first match with n )
        100 0 DO TABLE I + C@  OVER =
                 IF CR ." Its in the "  I . ." th cell. "
                    LEAVE THEN
               LOOP DROP  ;

Write SIGMA  a word which sums the numbers in in TABLE until a 0
is encountered.  It then prints the number of numbers and their
average. You will have to make provision for leaving loop early!

Ŀ
   Please Move to Lesson 5 Part 070 

