Fortran If construct


The if construct (called a block IF statement in FORTRAN 77) is common across many programming languages. It conditionally executes one block of code when a logical expression is evaluated to true.

[name:] IF (expr) THEN
[ELSE IF (expr) THEN [name]
[ELSE [name]
END IF [name]


  • name - the name of the if construct (optional)
  • expr - a scalar logical expression enclosed in parentheses
  • block - a sequence of zero or more statements or constructs

A construct name at the beginning of an if then statement must have the same value as the construct name at the end if statement, and it should be unique for the current scoping unit.

In if statements, (in)equalities and logical expressions evaluating a statement can be used with the following operators:

.LT.  which is <   ! less than
.LE.           <=  ! less than or equal
.GT.           >   ! greater than
.GE.           >=  ! greater than or equal
.EQ.           =   ! equal
.NE.           /=  ! not equal
.AND.              ! logical and
.OR.               ! logical or
.NOT.              ! negation


! simplest form of if construct
if (a > b) then
    c =  b / 2
end if
!equivalent example with alternate syntax

! named if construct
circle: if (r >= 0) then
    l = 2 * pi * r
end if circle

! complex example with nested if construct
block: if (a < e) then
    if (abs(c - e) <= d) then
        a = a * c
        a = a * d
    end if
    a = a * e
end if block

A historical usage of the if construct is in what is called an "arithmetic if" statement. Since this can be replaced by more modern constructs, however, it is not covered here. More details can be found here.