Chapter 5
Essentials of Essentials of Fortran-90/95/2003
Logical Expressions and Simple Decisions

Math 60 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 60 Homepage | Return to Brief Contents Page]
[Return to Full Contents Page]

Contents


5.1 Logical Expressions and Operators

Fortran uses the term logical to designate variables, constants and expressions whose values are either `true' or `false.' Mathematicians and some other computer languages refer to these as "boolean" values. Since special symbols were non-existent on card punch machines, in early versions of Fortran logical operators had to be represented by letters instead of symbols. However, to distinguish between letter combinations used as logical and relational operators and letter combinations used as "normal" variables, Fortran required that logical and relational operators and constants be surrounded by periods. Fortran-90/95/2003 now permits symbols to be used as relational operators as well as the older abbreviations.

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.

Fortran-77Fortran-90/95/2003 C/C++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:

        .TRUE.     .FALSE.
(Note that the surrounding periods are necessary!)

The other operators are:

Fortran C++Meaning
.AND. && logical and
.OR. || logical or
.NOT. ! logical not
.EQV. logical equivalence
.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,

As is common for the rules of logic, .NOT. is evaluated before the others, .AND. takes precedence over .OR., and .EQV. and .NEQV. are performed last.

5.2 Simple Choice: IF Statement

The Fortran "block" IF structure performs the same way in Fortran as in other popular, structured languages, but in Fortran, one uses the keyword THEN after the condition, and the keyword ENDIF to conclude the structure. For example,
        IF (A < B) THEN
	   block_1
	ELSE
	   block_2
	END IF
NOTE 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 IF
If 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
		ENDIF
NOTE: If the second IF were placed on a separate line (and not combined with the first ELSE), then two separate ENDIFs would be necessary.

5.3 One Line IF

If one needs to do only one thing (e.g. a subroutine call, an assignment statement), based on the evaluation of a logical expression, one can use the one statement version of the logical IF.

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 = -A
This 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:

Information about these statements may be found in sections 18.2 and 18.3.

5.4 What Happens If ...?

Suppose this were written in a program code (using Fixed Source Code form):
        IF (2    ..GE..     1) THEN
What happens? Is it an error?

Since Fixed Source Code form ignores all blanks, this statement is equivalent to

        IF (2. .GE. .1) THEN
which 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. dsmolarski@math.scu.edu
© Copyright 1998, 1999, 2000, 2005, Dennis C. Smolarski, S.J., All rights reserved.
Last changed: 5 June 2005.