HP Fortran Programmer's Reference (September 2007)

Execution control
Control constructs and statement blocks
Chapter 6 145
IF construct
The IF construct selects between alternate paths of execution. The executing path is
determined by testing logical expressions. At most, one statement block within the IF
construct executes.
Syntax
[
construct-name
:] IF (
logical-expression1
) THEN
statement-block1
[ELSE IF (
logical-expression2
) THEN [
construct-name
]
statement-block2
]
.
.
.
[ELSE [
construct-name
]
statement-block3
]
END IF [
construct-name
]
Execution logic
1.
logical-expression1
is evaluated. If it is true,
statement-block1
executes.
2. If
logical-expression1
evaluates to false and ELSE IF statements are present, the
logical-expression
for each ELSE IF statement is evaluated. The first expression to
evaluate to true causes the associated
statement-block
to execute.
3. If all expressions evaluate to false and the ELSE statement is present, its
statement-block
executes. If the ELSE statement is not present, no statement block
within the construct executes.
4. The normal flow of execution resumes with the first executable statement following the
END IF statement.
Example
! Compare two integer values
IF ( num1 < num2 ) THEN
PRINT *, ”num1 is smaller than num2.”
ELSE IF ( num1 > num2 ) THEN
PRINT *, ”num1 is greater than num2.”
ELSE
PRINT *, ”The numbers are equal”
END IF