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
||not equal to
||less than or equal to
||greater than or equal to
These logical operators are used to compare two values of the same
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
|(speed1 .gt. 50.0)
|(speed1 .ge. speed2)
|(speed1 .ne. speed2)
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
||Changes the value of the expression to the opposite value
||True only if both logical expressions are true
||True if either logical expression is true
||True if only one expression is true (exclusive or)
||True if the expressions have the same truth value
||True if the expressions do not have the same truth value
- 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
- arithmetic operators (**, *, /, +, -)
- relational operators (.lt. , .gt. , .eq. , .ne. , .le. , .ge.)
- .eqv. and .neqv.
Within the same level of priority, evaluation will proceed left to right.
1. Temperature Program
- Program Description
- This program asks for the temperature. Based on the results of
two logical expressions,
- it determines whether the temperature entered is greater than 80
degrees or less than 80
- Input/Output Description
- The user inputs the temperature.
- The output states whether the temperature entered is greater than 80
degrees or less than 80
- Prompt for and get the temperature.
- Compare the temperature with 80 degrees.
- Print a statement that compares the entered temperature with 80
- 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
to the second;
- assigns a value of .false. to the first expression and
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.)
3. Given that date and age are integers, which of the following are
FORTRAN expressions? (Hint: only one of the statements is a legal FORTRAN
expression. Explain why the other three are not.)
- 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)
- 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 .and. b
||a .or. b
||a .xor. b
||a .eqv. b
||a .neqv. b
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)