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 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 (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.
IF (x > 0) THEN WRITE(*,*) '+' ELSE IF (x == 0) THEN WRITE(*,*) '0' ELSE WRITE(*,*) '-' END IF
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
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.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
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