The following table indicates the standard relational operators available in Fortran (both earlier versions and Fortran-90/95/2003), the C/C++ equivalent, and the meaning.
|.EQ.||==||==||is equal to|
|.LT.||<||<||is less than|
|.LE.||<=||<=||is less than or equal to|
|.GT.||>||>||is greater than|
|.GE.||>=||>=||is greater than or equal to|
|.NE.||/=||!=||is not equal to|
The logical constants are:
The other operators are:
|.NEQV.||logical not equivalent|
As in some other programming languages, logical expressions must be surrounded by parentheses.
The standard rules of logic hold for evaluating Fortran logical expressions. Thus if A and B are logical expressions,
IF (A < B) THEN block_1 ELSE block_2 END IFNOTE that the concluding END IF may be spelled as one word or two on most systems.
As many statements as needed can be included in block_1 or block_2. If the boolean expression evaluates to .TRUE., then all the statements in block_1 are performed and block_2 is ignored. If the boolean expression evaluates to .FALSE., then block_2 is done and block_1 is ignored. After executing the statements in the chosen block, control then transfers to the next statement following the ENDIF.
The ELSE clause may be omitted, for example,
IF (A == B) THEN ... END IFIf repeated conditions need to be checked, one can expand the ELSE section by adding another IF on the same line, and using only one ENDIF. As many ELSE IF lines as needed can be used. For example,
IF (A < B) THEN block_1 ELSE IF (A > C) THEN block_2 ELSE block_3 ENDIFNOTE: If the second IF were placed on a separate line (and not combined with the first ELSE), then two separate ENDIFs would be necessary.
Its form is as follows:
IF ( logical condition ) statement
In this case, if the logical condition evaluates to .TRUE. the statement is executed. If the condition evaluates to .FALSE. the statement is ignored and control continues with the next statement following.
Note that the THEN is not included and, in its place, one puts a simple statement to condense an IF statement to one line. For example,
IF (A <= 0 ) A = -AThis line sets A equal to its absolute value.
This statement can be used in Fortran-90/95/2003 when the code needs to exit out of an infinite loop (see section 6.11).
There are two "antique" IF statements still permitted, both of which are being phased out, and their use is discouraged:
IF (2 ..GE.. 1) THENWhat happens? Is it an error?
Since Fixed Source Code form ignores all blanks, this statement is equivalent to
IF (2. .GE. .1) THENwhich is considered to be a legal statement and is interpreted according to its standard meaning.
This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1998, 1999, 2000, 2005, Dennis C. Smolarski, S.J., All rights reserved.
Last changed: 5 June 2005.