15.1.9 IF and IF DEFINED Directives

The IF and IF DEFINED directives specify a conditional compilation construct. IF tests whether a logical expression is .TRUE. or .FALSE.. IF DEFINED tests whether a symbol has been defined. [See Note]

The directive-initiated construct takes the following form:

cDEC$ IF (expr)    [or cDEC$ IF DEFINED (name)]
      block
[cDEC$ ELSE IF (expr)
      block]...
[cDEC$ ELSE
      block]
cDEC$ ENDIF


c
Is one of the following: C (or c), !, or * (see Section 15.1.1).


expr
Is a logical expression that evaluates to .TRUE. or .FALSE..


name
Is the name of a symbol to be tested for definition.


block
Are executable statements that are compiled (or not) depending on the value of logical expressions in the IF directive construct.

Rules and Behavior

The IF and IF DEFINED directive constructs end with an ENDIF directive and can contain one or more ELSEIF directives and at most one ELSE directive. If the logical condition within a directive evaluates to .TRUE. at compilation, and all preceding conditions in the IF construct evaluate to .FALSE., then the statements contained in the directive block are compiled.

A name can be defined with a DEFINE directive, and can optionally be assigned an integer value. If the symbol has been defined, with or without being assigned a value, IF DEFINED (name) evaluates to .TRUE.; otherwise, it evaluates to .FALSE..

If the logical condition in the IF or IF DEFINED directive is .TRUE., statements within the IF or IF DEFINED block are compiled. If the condition is .FALSE., control transfers to the next ELSEIF or ELSE directive, if any.

If the logical expression in an ELSEIF directive is .TRUE., statements within the ELSEIF block are compiled. If the expression is .FALSE., control transfers to the next ELSEIF or ELSE directive, if any.

If control reaches an ELSE directive because all previous logical conditions in the IF construct evaluated to .FALSE., the statements in an ELSE block are compiled unconditionally.

You can use any Fortran logical or relational operator or symbol in the logical expression of the directive, including: .LT., <, .GT., >, .EQ., ==, .LE., <=, .GE., >=, .NE., /=, .EQV., .NEQV., .NOT., .AND., .OR., and .XOR.. The logical expression can be as complex as you like, but the whole directive must fit on one line.

Examples

Consider the following:

 !  When the following code is compiled and run,
 !  the output depends on whether one of the expressions
 !  tests .TRUE., or all test .FALSE.

 !DEC$ DEFINE   flag=3
 !DEC$ IF (flag .LT. 2)
    WRITE (*,*) "This is compiled if flag less than 2."
 !DEC$ ELSEIF (flag >= 8)
    WRITE (*,*) "Or this compiled if flag greater than    &
                 or equal to 8."
 !DEC$ ELSE
    WRITE (*,*) "Or this compiled if all preceding        &
                 conditions .FALSE."
 !DEC$ ENDIF

Note: Each directive in the construct can begin with !MS$ instead of cDEC$.

For More Information:


Previous Page Next Page Table of Contents