Read the Web Notes on IF statements and an application of IF statements (interpolation). Finish Homework 5 and Start Homework 6
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,l3This 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.
.gt. [>] greater than .lt. [<] less than .ge. [>=] greater than or equal to .le. [<=] less than or equal to .eq. [==] equal to .ne. [/=] not equal toThe 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,zThe results are that l1 is false, l2 and l3 are true. The Fortran 90 equivalent statements for the last three above are: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
l1 = x > y
l2 = y < z
l3 = x /= y
Logical and arithmetic statements can be combined:
l3 = 2*(x+z) < yThe 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
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
real x,y,zThe results are that l1 is false, l2 and l3 are truelogical 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)
Check you knowledge of this material, but first be sure your Web Browser works correctly.
Written and Maintained by John Mahaffy : jhm@psu.edu