Logical Expressions

In FORTRAN, we will sometimes need to use logical expressions. These typically involve comparisons such as:

We can also have logical expressions that simply test the value of a LOGICAL variable.

In each case, a logical expression will have one of the two values .TRUE. and .FALSE..


How do we write comparisons?

If we are writing a comparison, we can use a variety of operators:

         Meaning                     Symbol            Abbreviation
    
         Less Than                     <                   .LT.

         Greater Than                  >                   .GT.

         Less Than or Equal To         <=                  .LE.

         Greater Than or Equal To      >=                  .GE.

         Equal To                      ==                  .EQ.

         Not Equal To                  /=                  .NE.

The abbreviations are available because FORTRAN is an old language. When it was invented, keyboards did not always have keys for symbols such as '<' or '>'.

We write a comparison in the format:

          (expression operator expression)


Some examples of logical expressions

Here Name must be a CHARACTER variable of some length and Done_Flag must be a LOGICAL variable.


How are simple logical expressions evaluated?

To evaluate a logical expression, first replace each variable by its value, and then do any necessary arithmetic. After you have two simple values (INTEGER, REAL or CHARACTER), you can apply the comparison operator.

If we have a comparison involving an INTEGER value and a REAL value, the INTEGER value is (temporarily) changed to a REAL value first.

If we are comparing two CHARACTER values of the same length, we compare them one character at a time, left to right, until we find two corresponding characters that are different. Two individual characters are compared using the ASCII character set, in which:

Blank comes before digits '0' through '9', which come before letters 'A' through 'Z', which come before letters 'a' through 'z'.

It is not necessary in this course to memorize the ASCII character set.

If we have a comparison involving two CHARACTER values of different lengths, the shorter one will be padded (on the right side) with blanks to make them the same length.


Compound logical expressions

In ordinary language, we often use more than one logical expression at a time, and we tie them together with "and" and "or". The same can be done in FORTRAN using .AND. and .OR., respectively.

It is important that we all agree on the meaning of "and" and "or". In logic, if we have "P and Q" (where P and Q are logical expressions), we regard the compound expression as true if both P and Q are true, and false otherwise. Likewise, the compound expression "P or Q" is regarded as false if both P and Q are false, and true otherwise.

If we put these in a table, we have:

          P         Q      P and Q    P or Q
         ------------------------------------ 
         true     true      true       true
         true     false     false      true
         false    true      true       true
         false    false     false      false

For example, the expression ((Age < 30) .AND. (Sex == 'M')) will have the value .TRUE. if both simple expressions (Age < 30) and (Sex == 'M') are true. That is, it is true for young males and false otherwise.

Likewise, the expression ((Height > 80) .OR. (Width > 80)) will have the value .TRUE. if at least one of the two simple expressions (Height > 80) and (Width > 80) is true.

We have three other operators for logical expressions, but they are usually not as useful as .OR. and .AND..

The operator .NOT. simply switches the value. If P is an expression, then .NOT. P has the opposite value.

The operator .EQV. refers to "equivalence". If P and Q are expressions, then (P .EQV. Q) is true if P and Q have the same value, both .TRUE. or both .FALSE.

The operator .NEQV. refers to "non-equivalence". If P and Q are expressions, then (P .NEQV. Q) is true if P and Q have different values, one .TRUE. and the other .FALSE..


Miscellaneous notes

All of the terms in FORTRAN that are involved in logic start and end with periods (.TRUE, .AND., etc.).

While all these operations make sense for LOGICAL variables, we do not often use LOGICAL variable. We do make use of logical expressions in IF statements and DO WHILE loops.

The meaning of "or" we are using here is sometimes called the "inclusive or". There is a second meaning, the "exclusive or", which is usually expressed in English with "either-or", that is, "one or the other but not both". FORTRAN does not have an operator for "exclusive or".

If we have a compound logical expression with several logical operator involved, these are evaluated in the following order:

  1. replace variables with their values
  2. do any necessary arithmetic
  3. apply comparison operators
  4. apply the .NOT. operator
  5. apply all .AND. operators, left to right
  6. apply all .OR. operators, left to right
  7. apply all .EQV. and .NEQV. operators (equally important), left to right

If that seems intimidating, bear in mind that most of the time we are dealing with fairly simple situations.