if (logical expression) executable statement
This has to be written on one line. This example finds the
absolute value of x:
if (x .LT. 0) x = -x
If more than one statement should be executed inside the if,
then the following syntax should be used:
if (logical expression) then
statements
endif
The most general form of the if statement has the
following form:
if (logical expression) then
statements
elseif (logical expression) then
statements
:
:
else
statements
endif
The execution flow is from top to bottom. The conditional expressions
are evaluated in sequence until one is found to be true. Then the
associated statements are executed and the control resumes
after the endif.
if (x .GT. 0) then
if (x .GE. y) then
write(*,*) 'x is positive and x >= y'
else
write(*,*) 'x is positive but x < y'
endif
elseif (x .LT. 0) then
write(*,*) 'x is negative'
else
write(*,*) 'x is zero'
endif
You should avoid nesting many levels of if statements
since things get hard to follow.
Copyright © 1995-7 by Stanford University. All rights reserved.