The THEN part and the ELSE part, if any, can contain one or more IF-THEN-ELSE-END IF statement in one of the three forms. That is, when you feel it is necessary, you can use as many IF-THEN-ELSE-END IF statements in the THEN part and the ELSE part as you want. However, please note that any such IF-THEN-ELSE-END IF must be fully contained in the THEN part or the ELSE part. If you follow the box trick, this requirement is automatically satisfied. The following is an example:
IF (logical-expression) THEN
statements
IF (logical-expression) THEN
statements
ELSE
statements
END IF
statements
ELSE
statements
IF (logical-expression) THEN
statements
END IF
statements
END IF
Let us start testing if x is positive. What we get is the following:
| x > 0 | display + |
| one down (i.e., +) two to go (i.e., - and 0) |
In the lower part, no decision can been reached. What we want to know is finding out is x is zero or negative (x cannot be positive here since it has been ruled out in the upper part). To determine whether a - or a 0 should be displayed, one more decision is required:
| x < 0 | display - |
| display 0) |
Since this is the work for the lower rectangle, let us put it there yielding the following:
| x > 0 | display + | |
| x < 0 | display - | |
| display 0 | ||
Converting to a IF-THEN-ELSE-END IF construct is easy since the above box is almost identical to that. So, here is our answer:
IF (x > 0) THEN
WRITE(*,*) '+'
ELSE
IF (x < 0) THEN
WRITE(*,*) '-'
ELSE
WRITE(*,*) '0'
END IF
END IF
Obviously, this problem cannot be solved with a two-way IF and the box trick becomes useful. Let us start with x<0.
| x < 0 | display -x |
| here we have x >= 0 |
For the x >= 0 part, x may be in the range of 0 and 1; if not, x must be greater than 1 since x cannot be less than 0. Therefore, we have the following box for the case of x >= 0:
| x <= 1 | x is in the range of 0 and 1. display x*x |
| here we have x > 1. display 2*x |
Inserting this box into the previous one yields the following final result:
| x < 0 | display -x | |
| x <= 1 | display x*x | |
| display 2*x | ||
Converting to using IF, we have the following:
IF (x < 0) THEN
WRITE(*,*) -x
ELSE
IF (x <= 1) THEN
WRITE(*,*) x*x
ELSE
WRITE(*,*) 2*x
END IF
END IF
There are many solutions to this problem; but, we shall use the box trick again. Let us pick two numbers, say a and b. Thus, we get the following:
| a < b | a has the potential to be the smallest |
| since b <= a, b has the potential to be the smallest |
Now we know a possible smallest number. To find the real smallest one, this "possible" number must be compared against c. If the possible one is a (the upper part), we need to do the following:
| a < c | a is the smallest |
| since c <= a and b <= a, c is the smallest |
Let us turn to the lower part, where b has the potential to be the smallest. Comparing with c yields:
| b < c | b is the smallest |
| since c <= b and b <= a, c is the smallest |
Inserting the above two boxes into the first one yields the following complete solution:
| a < b | a < c | the smallest is a |
| the smallest is c | ||
| b < c | the smallest is b | |
| the smallest is c |
Converting to the IF version, we have
IF (a < b) THEN
IF (a < c) THEN
Result = a
ELSE
Result = c
END IF
ELSE
IF (b < c) THEN
Result = b
ELSE
Result = c
END IF
END IF
WRITE(*,*) 'The smallest is ', Result
The above code segment uses variable Result to hold the
smallest value.