home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l5p060 < prev    next >
Text File  |  1990-07-15  |  5KB  |  111 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 5 Part 060  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.          ┌──────────────────────────────────────────┐
  6.          │  A Look at the Varieties of DO ... LOOPS │
  7.          └──────────────────────────────────────────┘
  8.  
  9. The phrase step 1, step 2, and step 3, represent a sequence of
  10. Forth words.  limit, init, and n are single numbers placed on
  11. the parameter stack.
  12.  
  13.    ... step 1  limit init   DO  step 2      LOOP  step 3 ...
  14.    ... step 1  limit init   DO  step 2  n  +LOOP  step 3 ...
  15.    ... step 1  limit init  ?DO  step 2      LOOP  step 3 ...
  16.    ... step 1  limit init  ?DO  step 2  n  +LOOP  step 3 ...
  17.  
  18. Step 1: is executed once before entering each of the loops
  19.  
  20. limit : is the maximum value of the loop counter.
  21. init  : is the initial value of the loop counter.
  22.  
  23. DO    : saves the maximum value of the loop counter and the
  24.         current or initial value ( usually on the return stack )
  25.         and continues execution with step 2.
  26. ?DO   : first check to see if limit is equal to init and if so
  27.         jumps directly to step 3. If limit and init are not equal
  28.         the action is the same as DO.
  29.  
  30. step 2: is executed once on each pass of the loop.
  31.  
  32. LOOP  : increments the loop counter by 1 and then compares the new
  33.         value of the loop counter with limit. If new loop counter
  34.         is equal to the loop limit the loop terminates and execution
  35.         continues with step 3. If the new loop counter is not equal
  36.         to the loop limit then the loop is repeated by performing
  37.         step 2 again.
  38. +LOOP : Increment the current value of the loop counter by the top
  39.         number on the data stack, n, ( instead of 1 ) and then perform
  40.         the same action as LOOP.
  41.  
  42. step 3: is executed once after completion of the loop.
  43.  
  44. Note:  In Forth 83  the initial value of the loop counter, the loop
  45. limit and the current value of the loop counter all all unsigned
  46. 16 bit integers.
  47.  
  48. ╓──────────────╖
  49. ║ Problem 5.9  ║
  50. ╙──────────────╜
  51. Given the following loop testing words:
  52. :  DOLOOP   DO  CR I . KEY? ?LEAVE     LOOP ; \ All of these words take
  53. :  DO+LOOP  DO  CR I . KEY? ?LEAVE  2 +LOOP ; \ the loop limit and the
  54. :  DO-LOOP  DO  CR I . KEY? ?LEAVE -2 +LOOP ; \ initial value on the
  55. : ?DOLOOP  ?DO  CR I . KEY? ?LEAVE     LOOP ; \ stack. ie ( l i   -- )
  56. : ?DO+LOOP ?DO  CR I . KEY? ?LEAVE  2 +LOOP ; \ KEY? ?LEAVE allows any
  57. : ?DO-LOOP ?DO  CR I . KEY? ?LEAVE -2 +LOOP ; \ key press to abort.
  58. Determine the output for the following stack inputs.
  59.  a) 10  8   b)  10 10   c)  10  12
  60.  d)  2 -2   e)  -2  2   f)  -1  -3    g) -3  -1
  61. Caution:  Think first!  Some may execute a long long time!!
  62.  
  63.                 ┌──────────────────────┐
  64.                 │ Leaving Loops Early. │
  65.                 └──────────────────────┘
  66.  
  67.   step 1  limit init   DO    step 2
  68.                              (condition)
  69.                              IF  step 3 LEAVE THEN
  70.                              step 4
  71.                        LOOP  step 5 ...
  72.  
  73. Execute step 1. Repeat loop as before executing step 2 and step 4 on
  74. each pass - except that if condition is true before loop is finished
  75. execute step 3 and leave loop to execute step 5.  Note:  step 4 will
  76. not be executed if we leave the loop early.
  77. Note:  EXIT cannot be used in DO LOOPs
  78.  
  79. This is an alternative form if step 3 is not required.
  80.   (condition) could be KEY? or any word that returns a flag.
  81.   step 1  limit init   DO    step 2
  82.                              (condition) ?LEAVE
  83.                              step 4
  84.                        LOOP  step 5
  85.  
  86. ╓───────────────╖
  87. ║ Problem 5.10  ║
  88. ╙───────────────╜
  89. Consider the following array manipulating words.  RND is your favorite
  90. random number generator   RND ( n -- r )  where  0 <= r < n
  91.  
  92. CREATE TABLE 100 ALLOT  \ this is a byte array!
  93. : FILL-TABLE  ( -- ) \ Fill table with random numbers.
  94.        100 0 DO 100 RND TABLE I + C! LOOP ;
  95. : SEE-TABLE   ( -- ) \ Display numbers in the table.
  96.        CR 100 0 DO    TABLE I + C@ 4 .R LOOP ;
  97. \ Example of leaving loop early.
  98. : MATCH  ( n   -- )  \ Search and report on first match with n )
  99.         100 0 DO TABLE I + C@  OVER =
  100.                  IF CR ." Its in the "  I . ." th cell. "
  101.                     LEAVE THEN
  102.                LOOP DROP  ;
  103.  
  104. Write SIGMA  a word which sums the numbers in in TABLE until a 0
  105. is encountered.  It then prints the number of numbers and their
  106. average. You will have to make provision for leaving loop early!
  107.  
  108. ┌────────────────────────────────────┐
  109. │   Please Move to Lesson 5 Part 070 │
  110. └────────────────────────────────────┘
  111.