if (logical expression) executable statementThis has to be written on one line. This example finds the absolute value of x:
if (x .LT. 0) x = -xIf more than one statement should be executed inside the if, then the following syntax should be used:
if (logical expression) then statements endifThe most general form of the if statement has the following form:
if (logical expression) then statements elseif (logical expression) then statements : : else statements endifThe execution flow is from top to bottom. The conditional expressions are evaluated in sequence until one is found to be true. Then the associated code is executed and the control jumps to the next statement 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' endifYou should avoid nesting many levels of if statements since things get hard to follow.
x+y if x and y are both positive x-y if x is positive and y negative y if x is negative 0 if x or y is zero