Skip to main content

5.1) More Than One Test


If you want to perform more than one test in a single part of an if statement then use .and. or use .or.. Fortran 77 uses the same .and. and .or. syntax. Here are some examples.

       if ( a > b .and. c < d ) then
          print *, ‘a is greater than b and c is less than d’
       endif

Line 1 can be written with more brackets but make sure you always have an open bracket ( just after the if and a close bracket ) just before the then, as in the above. With more brackets (if you think it may be clearer).

       if ( ( a > b ) .and. (c < d ) ) then

The above statement requires that both are satisfied in order to go into the if statement. If either are not satisfied then the program will move to the next part of the if statement, whether it be an elseif, else, or endif.