The most general form of the IF-THEN-ELSE-END IF statement is the following:
where statements-1 and statements-2 are sequences of executable statements, and logical-expression is a logical expression. The execution of this IF-THEN-ELSE-END IF statement goes as follows:IF (logical-expression) THEN statements-1 ELSE statements-2 END IF
INTEGER :: Number READ(*,*) Number IF (MOD(Number, 2) == 0) THEN WRITE(*,*) Number, ' is even' ELSE WRITE(*,*) Number, ' is odd' END IF
REAL :: X, Absolute_X
X = .....
IF (X >= 0.0) THEN
Absolute_X = X
ELSE
Absolute_X = -X
END IF
WRITE(*,*) 'The absolute value of ', x, &
' is ', Absolute_X
INTEGER :: a, b, Smaller
READ(*,*) a, b
IF (a <= b) THEN
Smaller = a
ELSE
Smaller = b
END IF
Write(*,*) 'The smaller of ', a, ' and ', &
b, ' is ', Smaller
Draw a rectangular box and a vertical line dividing the box into two parts. Then, write down the logical expression in the left part and draw a horizontal line dividing the right parts into two smaller ones. The upper rectangle is filled with what you want to do when the logical expression is .TRUE., while the lower rectangle is filled with what you want to do when the logical expression is .FALSE.:
| logical-expression | what you want to do when the logical expression is .TRUE. |
| what you want to do when the logical expression is .FALSE. |
For example, the third example above has the following description:
| a <= b | a is the smaller number |
| b is the smaller number |
Although this is an easy example, you will sense its power when you will be dealing with more complex problems.