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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 3 Part 080  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.         ┌─────────────────────────────────────────────────┐
  6.         │  Structured Programming -  IF ... ELSE ... THEN │
  7.         └─────────────────────────────────────────────────┘
  8.  
  9. Let's review the structured programming conditional decision phrases.
  10.  IF  ... THEN  and  IF ... ELSE ... THEN
  11.  
  12. The first thing to remember is that they can only be used within a word
  13. definition.  You must precede the  " IF " with a word phrase or condition
  14. that leaves a true or false flag on the parameter stack.  You should
  15. also be aware of the fact that any non zero value will behave the same
  16. as a true flag.  For  IF ... THEN  the setup is as follows:
  17.  
  18.      ....  step1  condition1  IF  step2  THEN  step3  ...
  19.  
  20. where:   step1        -may be some preliminary words.
  21.          condition1   -is a word or phrase that leaves a flag
  22.          step2        -is a word or phrase that is performed
  23.                        only if condition1 leaves a true flag.
  24.          step3        -execution continues here if condition1
  25.                        leaves a false flag and also after step2
  26.                        has been completed when condition1 leaves
  27.                        a true flag.
  28.  
  29. Some Forth programmers like to use a type of flow chart called D-CHARTS
  30. which were developed by Dijkstra.  D-Charts are used to diagram program
  31. flow and are especially good for illustrating the logic/decision paths
  32. in a program module (read Forth word).  Generally speaking program flow
  33. is from the top to the bottom of the page when using D-Charts.  Here is
  34. how the above IF ... THEN could be diagramed using a D-Chart.
  35.                         |
  36.                       step1
  37.                         |                   \/
  38.                     condition1              ||
  39.                         |                 program
  40.                        / \                  ||
  41.               false  /     \  true         flow
  42.                    /         \              ||
  43.                   |           |             \/
  44.                   |         step2
  45.                   |           |
  46.                    \         /
  47.                      \     /
  48.                        \ /
  49.                         |
  50.                       step3
  51.                         |
  52. Here is the setup for  ... IF ... ELSE ... THEN ...
  53.  ...  step1  condition1  IF  step2  ELSE  step3  THEN  step4  ...
  54. where:   step1        -may be some preliminary words.
  55.          condition1   -is a word or phrase that leaves a flag.
  56.          step2        -is a word or phrase that is performed
  57.                        only if condition1 leaves a true flag.
  58.          step3        -is a word or phrase that is performed
  59.                        only if condition1 leaves a false flag.
  60.          step4        -execution continues here after either
  61.                        step2 or step3 have been completed.
  62. The D-Chart for this setup would look like this.
  63.                         |
  64.                       step1
  65.                         |                   \/
  66.                     condition1              ||
  67.                         |                 program
  68.                        / \                  ||
  69.               false  /     \  true         flow
  70.                    /         \              ||
  71.                   |           |             \/
  72.                 step3       step2
  73.                   |           |
  74.                    \         /
  75.                      \     /
  76.                        \ /
  77.                         |
  78.                       step4
  79.                         |
  80. The complete D-Chart for the word TEST of lesson 3 Part 6 follows:
  81.              TEST  ( n -- )
  82.             >-----------+
  83.                         |
  84.                     display number
  85.                         |                   \/
  86.                    is number even?          ||
  87.                         |                 program
  88.                        / \                  ||
  89.               false  /     \  true         flow
  90.                    /         \              ||
  91.                   |           |             \/
  92.                display     display
  93.                 "odd"      "even"
  94.                   |           |
  95.                    \         /
  96.                      \     /
  97.                        \ /
  98.                         |
  99.                      display
  100.                      "number"
  101.                         |
  102.  
  103. ┌────────────────────────────────────┐
  104. │  Please move to Lesson 3 Part 090  │
  105. └────────────────────────────────────┘
  106.