# IF-THEN-ELSE IF-END IF

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:

```IF (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
```
Fortran evaluates logical-expression-1 and if the result is .TRUE., statements-1 is executed followed by the statement after END IF. If logical-expression-1 is .FALSE., Fortran evaluates logical-expression-2 and executes statements-2 and so on. In general, if logical-expression-n is .TRUE., statements-n is executed followed by the statement after END IF; otherwise, Fortran continues to evaluate the next logical expression.

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.

### Examples

• 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

IF (x < 50) THEN
ELSE IF (x < 60) THEN
ELSE IF (x < 70) THEN
ELSE IF (x < 80) THEN
ELSE
END IF
```
First, 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'.

The first and second examples show that IF-THEN-ELSE IF-END IF can save some space and at the same time make a program more readable. Compare these two solutions with those using nest IF.

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
```