Logical Structures


Assignment :

Read the Web Notes on IF statements and an application of IF statements (interpolation). Finish Homework 5 and Start Homework 6

New Fortran:

LOGICAL, .TRUE., .FALSE., .EQ., ==, .NE., /=, .LT., <, .LE., <=, .GT., >, .GE., >=, .EQV., .NEQV., .OR., .AND., .NOT.

Now that you can do basic calculations, its time you learned how to build a little intelligence into your program. We are going to use the logical functions of the CPU, so one starting point is to introduce a new Fortran data type, LOGICAL. Generally you create logical data types during the process of making comparisons, without ever having to assign a specific logical variable. However, if you want to store the results of a logical operation, you can declare variables to have type logical with the non-executable statement like:

      logical l1,l2,l3
This statement is located with your REAL and INTEGER statements in the program. The variables l1,l2, and l3 in this example can only take on the values true or false (1 or 0). This is a natural, situation for using a single bit as the element of storage. However, because of physical problems in dealing with single bits, the least space Fortran uses for storage is a single byte for each logical variable (on some machines such as Cray it must use more space). The Fortran constants corresponding to true and false are ".TRUE." and ".FALSE.". We could assign values to our variables with statements like:

      l1 = .true.
      l2 = .false.
      l3=.false.
Don't forget to use periods on both sides of the constants. This use of periods is common in syntax related to logical variables and operations.

Logical Relational Operators

There are six logical operators that perform comparisons between numbers and produce a logical result of ".TRUE." or ".FALSE." These are:

.gt.	[>]	greater than
.lt.	[<]	less than
.ge.	[>=]	greater than or equal to
.le.	[<=]	less than or equal to
.eq.	[==]	equal to
.ne.	[/=]	not equal to
The symbol expressions in the square brackets are permitted in Fortran 90, but not 77. Historically, Fortran preceded the existance of the symbols "<", and ">" on keyboards. Note that Fortran 90 requires two sequential equals signs for the "equal to" operation to distinguish from assignment statements. For the following Fortran:

      real x,y,z

logical l1,l2,l3 x=1.1 y=2.0 z=3.0 l1=x.gt.y l2= y.lt.z l3=x.ne.y

The results are that l1 is false, l2 and l3 are true. The Fortran 90 equivalent statements for the last three above are:

      l1 = x > y
      l2 = y < z
      l3 = x /= y
Logical and arithmetic statements can be combined:

      l3 = 2*(x+z) < y
The arithmetic operations are always all done before the logical operations.

If you want to compare two logical variables or expressions, you can't use ".eq." or ".ne.". The appropriate expressions are:

.eqv.	equivalent to
.neqv.	not equivalent to

Boolean Operators

You are used to sorting out expressions like: " the ball is red and the block is not blue," and "I am alive or I am dead". There is a formal way of dealing with logical comparisons "or" and "and", and logical negation, "not". This is Boolean algebra. In Fortran the Boolean operators are ".or.", ".and.", and ".not.". Note the bounding with periods again. ".and." and ".or." are called binary operators because they need logical expressions to the left and right to operate. ".not." is called unary because it only operates on a single logical variable to its right. Results of Boolean operations are presented in a standard "truth table".

operands        operations
l1      l2      .not.l1     l1.and.l2     l1.or.l2
true    true     false       true          true
true    false    false       false         true
false   true     true        false         true
false   false    true        false         false
For the following Fortran:

      real x,y,z

logical l1,l2,l3 x=1.1 y=2.0 z=3.0 l1=x.gt.y .and. y.lt.z l2= y.lt.z .or. x.gt.y l3=.not.(x.eq.y)

The results are that l1 is false, l2 and l3 are true

Precedence

Now that we have a bunch more operators to worry about, it is worth restating the order of Precedence of operators.

  1. ()
  2. **
  3. * /
  4. + -
  5. .EQ. .NE. .LT. .LE. .GT. .GE.
  6. .NOT.
  7. .AND.
  8. .OR.
  9. .EQV. .NEQV.


Check you knowledge of this material, but first be sure your Web Browser works correctly.


Back to the Table of Contents / Home


Written and Maintained by John Mahaffy : jhm@psu.edu