       ͻ
        Lesson 3 Part 080  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

        Ŀ
          Structured Programming -  IF ... ELSE ... THEN 
        

Let's review the structured programming conditional decision phrases.
 IF  ... THEN  and  IF ... ELSE ... THEN

The first thing to remember is that they can only be used within a word
definition.  You must precede the  " IF " with a word phrase or condition
that leaves a true or false flag on the parameter stack.  You should
also be aware of the fact that any non zero value will behave the same
as a true flag.  For  IF ... THEN  the setup is as follows:

     ....  step1  condition1  IF  step2  THEN  step3  ...

where:   step1        -may be some preliminary words.
         condition1   -is a word or phrase that leaves a flag
         step2        -is a word or phrase that is performed
                       only if condition1 leaves a true flag.
         step3        -execution continues here if condition1
                       leaves a false flag and also after step2
                       has been completed when condition1 leaves
                       a true flag.

Some Forth programmers like to use a type of flow chart called D-CHARTS
which were developed by Dijkstra.  D-Charts are used to diagram program
flow and are especially good for illustrating the logic/decision paths
in a program module (read Forth word).  Generally speaking program flow
is from the top to the bottom of the page when using D-Charts.  Here is
how the above IF ... THEN could be diagramed using a D-Chart.
                        |
                      step1
                        |                   \/
                    condition1              ||
                        |                 program
                       / \                  ||
              false  /     \  true         flow
                   /         \              ||
                  |           |             \/
                  |         step2
                  |           |
                   \         /
                     \     /
                       \ /
                        |
                      step3
                        |
Here is the setup for  ... IF ... ELSE ... THEN ...
 ...  step1  condition1  IF  step2  ELSE  step3  THEN  step4  ...
where:   step1        -may be some preliminary words.
         condition1   -is a word or phrase that leaves a flag.
         step2        -is a word or phrase that is performed
                       only if condition1 leaves a true flag.
         step3        -is a word or phrase that is performed
                       only if condition1 leaves a false flag.
         step4        -execution continues here after either
                       step2 or step3 have been completed.
The D-Chart for this setup would look like this.
                        |
                      step1
                        |                   \/
                    condition1              ||
                        |                 program
                       / \                  ||
              false  /     \  true         flow
                   /         \              ||
                  |           |             \/
                step3       step2
                  |           |
                   \         /
                     \     /
                       \ /
                        |
                      step4
                        |
The complete D-Chart for the word TEST of lesson 3 Part 6 follows:
             TEST  ( n -- )
            >-----------+
                        |
                    display number
                        |                   \/
                   is number even?          ||
                        |                 program
                       / \                  ||
              false  /     \  true         flow
                   /         \              ||
                  |           |             \/
               display     display
                "odd"      "even"
                  |           |
                   \         /
                     \     /
                       \ /
                        |
                     display
                     "number"
                        |

Ŀ
  Please move to Lesson 3 Part 090  

