## 5 LOGICAL EXPRESSIONS

### Logical Expressions: Relational Operators

A logical expression can only have the values .true. or .false.. Relational operators are used to form logical expressions to determine choice and decision-making structures. Table 1 shows the relational operators and their meanings.

Table 1: Relational Operators
Operator Meaning
.eq. equal to
.ne. not equal to
.lt. less than
.le. less than or equal to
.gt. greater than
.ge. greater than or equal to

These logical operators are used to compare two values of the same type.

Example

Given that the REAL variable speed1 has the value 83.4 and the REAL variable speed2 has the value 96.3 Table 2 shows possible expressions and their values.

Table 2: Relational Operators
Expression Value
(speed1 .gt. 50.0) .true.
(speed1 .ge. speed2) .false.
(speed1 .ne. speed2) .true.

### Logical Expressions: Logical Operators

There are also logical operators which are used only between complete logical expressions. (.NOT. operates on one logical expression; all the rest operate on two.) Table 3 shows the logical operators and their meanings.

Table 3: Logical Operators
Operator Meaning
.not. Changes the value of the expression to the opposite value
.and. True only if both logical expressions are true
.or. True if either logical expression is true
.xor. True if only one expression is true (exclusive or)
.eqv. True if the expressions have the same truth value
.neqv. True if the expressions do not have the same truth value

Example

If a = 3.0 and b = 8.0, then

.not. (a .lt. b) has the value .false.

(a .ne. b) .and. (a .lt. b) has the value .true. (both logical expressions are true)

(a .lt. b) .or. (a .gt. b) has the value .true. (one of the expressions is true)

(a .ne. b) .xor. (a .lt. b) has the value .true. (both expressions are true)

(a .lt. b) .eqv. (a .gt. b) has the value .false. (the truth values are not equivalent)

(a .lt. b) .neqv. (a .gt. b) has the value .true. (the truth values are not equivalent)

The order of precedence for operators from highest to lowest is

1. parentheses
2. arithmetic operators (**, *, /, +, -)
3. relational operators (.lt. , .gt. , .eq. , .ne. , .le. , .ge.)
4. .not.
5. .and.
6. .or.
7. .eqv. and .neqv.

Within the same level of priority, evaluation will proceed left to right.

### EXERCISES

1. Temperature Program
Program Description
This program asks for the temperature. Based on the results of two logical expressions, it
it determines whether the temperature entered is greater than 80 degrees or less than 80
degrees.

Input/Output Description
The user inputs the temperature.

The output states whether the temperature entered is greater than 80 degrees or less than 80
degrees.
Algorithm
1. Prompt for and get the temperature.
2. Compare the temperature with 80 degrees.
3. Print a statement that compares the entered temperature with 80 degrees.
Code
Temperature Program

Enter, compile, and execute the program. Input a value for the variable temp that

• assigns a value of .true. to the first expression and .false. to the second;
• assigns a value of .false. to the first expression and .true. to the second; and,
• assigns a value of .false. to both statements.

2. In the following exercise, given the following type and assignment statements, determine the value of the expressions (.true. or .false.) if year = 1974, mass = 2.345, start = 5, weight = 234.23, and pi = 3.14. (Only one of the expressions is false.)

INTEGER year, start
REAL mass, weight, pi

A. (2.0 * pi) .ne. 180.0
B. 200.0 .le. (weight -mass)
C. year .gt. (start *(20+start))
D. (start**4) .ne. (year-1900)
E. 10.0 * mass .gt. weight
F. (pi **start) .ge. (3.0*pi)7. (pi**2.0) .eq. (pi*pi)
G. (year - start) .ne. (start +1974)
3. Given that date and age are integers, which of the following are legal FORTRAN expressions? (Hint: only one of the statements is a legal FORTRAN expression. Explain why the other three are not.)
A. date - (age. eq. date) + age
B. (date -age) .eq. (date + age)
C. (date - age .eq. date) + age
D. ((date - age) .eq. date) + age

4. Complete the following truth table.

Table 4: Logical Operators Truth Table
A B .not.a a .and. b a .or. b a .xor. b a .eqv. b a .neqv. b
T T

T F

F T

F F

5. Given the following declarations and initializations, what is the value of each logical expression?

INTEGER age
LOGICAL old

old = .false.
age = 46
A. (.not. old) .or. (age .lt. 25)
B. (.not. old) .and. (age .lt. 25)
C. .not. (age .lt. 25)
D. (age .lt. 50) .and. (.not. old)
E. old .or. (.not. old)
F. old .neqv. (age .le. 50)