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

- Suppose we need a program segment to read a number
**x**and display its sign. More precisely, if**x**is positive, a**+**is displayed; if**x**is negative, a**-**is displayed; otherwise, a**0**is displayed. With an**IF-THEN-ELSE-END IF**statement, we have a two-way decision (*i.e.*, true or false). What we need is a tree-way decision and some trick is required. In this case, the*box trick*can be very helpful.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

- Given a
**x**, we want to display the value of**-x**if**x < 0**, the value of**x*x**if**x**is in the range of 0 and 1 inclusive, and the value of**2*x**if**x**is greater than 1.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

- Given three numbers
**a**,**b**and**c**, we want to find out the smallest one.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 smallestsince **b <= a**,**b**has the potential to be the smallestNow 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 smallestsince **c <= a**and**b <= a**,**c**is the smallestLet us turn to the lower part, where

**b**has the potential to be the smallest. Comparing with**c**yields:**b < c****b**is the smallestsince **c <= b**and**b <= a**,**c**is the smallestInserting 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 haveIF (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

**Result**to hold the smallest value.