The Nested **IF-THEN-ELSE-END IF** statement could produce a deeply
nested **IF** statement which is difficult to read. There is
a short hand to overcome this problem. It is the
**IF-THEN-ELSE IF-END-IF** version. Its syntax is shown below:

Fortran evaluatesIF (logical-expression-1) THEN statements-1 ELSE IF (logical-expression-2) THEN statements-2 ELSE IF (logical-expression-3) THEN statement-3 ELSE IF (.....) THEN ........... ELSE statements-ELSE END IF

If all logical expressions are **.FALSE.** and if **ELSE** is there,
Fortran executes the *statements-ELSE*; otherwise, Fortran executes
the statement after the **END IF**.

Note that the statements in the **THEN** section, **ELSE IF** section,
and **ELSE** section can be another **IF** statement.

- 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. Here is a possible solution using**IF-THEN-ELSE IF-END IF**:IF (x > 0) THEN WRITE(*,*) '+' ELSE IF (x == 0) THEN WRITE(*,*) '0' ELSE WRITE(*,*) '-' 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.The following is a possible solution:

IF (x < 0) THEN WRITE(*,*) -x ELSE IF (x <= 1) THEN WRITE(*,*) x*x ELSE WRITE(*,*) 2*x END IF

- Consider the following code segment:
INTEGER :: x CHARACTER(LEN=1) :: Grade IF (x < 50) THEN Grade = 'F' ELSE IF (x < 60) THEN Grade = 'D' ELSE IF (x < 70) THEN Grade = 'C' ELSE IF (x < 80) THEN Grade = 'B' ELSE Grade = 'A' END IF

**x**is less than 50,**'F'**is assigned to**Grade**. If**x**is greater than or equal to 50, the execution continue with the first**ELSE IF**where**x < 60**is tested. If it is**.TRUE.**,**'D'**is assigned to**Grade**. Note that one can reach the test of**x < 60**simply because the test**x < 50**is**.FALSE.**. Therefore, when reaches**x < 60**, we are sure that**x >= 50**must hold and as a result,**Grade**receives**'D'**if**x**is greater than or equal to 50 and is less than 60.By the same token, we know that if

**x**is greater than or equal to 60 and is less than 70,**Grade**receives**'C'**. If**x**is greater than or equal to 70 and is less than 80,**Grade**receives**'B'**. Finally, if**x**is greater than or equal to 80,**Grade**receives**'A'**.

Note also that not all nested **IF** can be converted to the
**IF-THEN-ELSE IF-ELSE-END-IF** form. For example, the example of
determining the smallest of three numbers cannot be converted immediately.
In general, if all tests (*i.e.*, logical expressions) are
mutually exclusive, then the chance to have a successful conversion is high.
Otherwise, rewriting some parts or combining logical expression can be
helpful. Here is one more example:

Let us reconsider the problem of finding the smallest of three
given numbers. We know that if **a** is the smallest, then
it must be smaller than the other two. Moreover, the condition for
a number being the smallest is mutually exclusive. Thus, we have
a successful conversion as follows:

IF (a < b .AND. a < c) THEN Result = a ELSE IF (b < a .AND. b < c) THEN Result = b ELSE Result = c END IF