The IF construct controls whether a block of statements or executable constructs will be executed based on the value of a logical expression.
The IF-THEN statement signals the beginning of an IF construct.
The ELSE IF statement controls execution of a block of code where all previous IF expressions in the construct were false.
The ELSE statement controls execution of a block of code where all other IF expressions in the construct were false.
The END IF statement signals the end of the innermost nested IF construct.
Syntax[construct-name:] IF (expr) THEN block [ELSE IF (expr) THEN [construct-name] block] ... [ELSE [construct-name] block] END IF [construct-name]Where:
construct-name is an optional name for the construct.
expr is a scalar LOGICAL expression.
block is a sequence of zero or more statements or executable constructs.
The exprs are evaluated in the order of their appearance in the construct until a true value is found, or an ELSE statement or END IF statement is encountered. If a true value is found, the block immediately following is executed and this completes the execution of the construct. The exprs in any remaining ELSE IF statements of the IF construct are not evaluated.
If none of the evaluated expressions is true, then the block of code following the ELSE statement is executed. If there is no ELSE statement, the execution of the construct is completed without the execution of any block within the construct.
The ELSE statement and its block of code must be the last block to appear in the IF construct.
If the IF statement specifies a construct name, the corresponding END IF statement must specify the same construct name. If the IF statement does not specify a construct name, the corresponding END IF statement must not specify a construct name.
Example 1integer :: i=0 if (i > 10) then write(*,*) "i is greater than ten" else if (i > 0) then write(*,*) "i is less than ten but greater than zero" else if (i < 0) then write(*,*) "i is less than zero" else write(*,*) "i equals zero" end if
Example 2logical :: exp1=.true.,exp2=.false. outer_if: & if (exp1) then inner_if: & if(exp2) then end if inner_if end if outer_if if(exp1 .eqv. exp2) then end if