# Logical expressions

February 2020

Back to index.html.

# Logical Expressions

Fortran has several logical expressions (or Boolean expression), which yield either true or false. This function is mainly used in a conditional like an if statement introduced in the later chapter. Here I introduce some logical operators.

## Logical values

Fortran has unique literals to express the logical status, true and false.

Value Meaning
.true. the status of true
.false. the status of false

It looks weird, but it is a valid literal (non-numerical value). In fact, you can show the logical value in print; T for true and F for false.

program bool
print *,.true.
print *,.false.
end program bool
 T
F

## Relational (comparison) operators

There are some operators to compare two values and return .true. or .false..

Operator Meaning Example
> Greather than 10 > 5 … true
< Less than 10 < 5 … false
>= Greater than or equal to 10 <= 5 … true
<= Less than or equal to 10 <= 5 … true
== Equal to 10 == 5 … false
/= Unequal to 10 /= 5 … true
program bool
print *,10>9
print *,10<5
print *,10>=9
print *,10<=5
print *,10==5
print *,10/=5
end program bool
 T
F
T
F
F
T

Of course, you can use the relational operators with variables.

program bool
integer :: x,y
x = 10
y = 20
print *,x>9
print *,x<x+1
print *,x==y
end program bool

## Combinational (Boolean) operators

Fortran also has combinational (Boolean) operators. Each operator accepts logical expressions, combines them, and returns .true. or ,false.. The .not. operator takes 1 expression, but the others take 2 expressions.

Operator Meaning
.and. Logical And; ,true. if both expressions are true
.or. Logical Or; ,true. if either expression is true
.not. Logical Not; returns the opposite value

The expressions can be encircled with ().

program bool
print *,10>9 .and. 10>=9
print *,(10<5).or.(10>5)
print *,.not.(10==5)
end program bool
 T
T
T
T

The precedence of the Boolean operators is (), .not., .and., and or. (because of the set theory). I recommend you always to use () accordingly, when two or more expressions are combined.

program bool
print *,10>9 .and. .not. 10>=9 .or. 10==9
print *,(10>9 .and. (.not.10>=9)) .or. 10==9  ! recommended
end program bool

## Logical equivalence

The operator == compares two numerical values and returns the equivalence of the values. If you have to know the equivalence between two logical expressions (not the numerics), .eqv. is the appropriate operator. Similarly, .neqv. is corresponding to /= (not equal) for the logical expressions.

program bool
print *,(10>9) .eqv. .true. ! test if (10>9) is true, or not
! you can not write it using ==.
print *,(10<5).eqv.(10>5)
print *,(10<5).neqv.(10>5)
end program bool
 T
F
T

In addition to .eqv. and .neqv., you can apply any logical operators (like .and., .or., and .not) to any logical expressions.

## Precedence of logical operators

There is a clear rule for the precedence of logical operators. When you give a formula mixing several operators, the Fortran compiler interprets it as follows (explained in Chapman, 2008).

1. First, all arithmetic operators (+,-,*,/,**) are evaluadted.
2. All relational operators (==,/=,>,>=,<,<=) are evaluated from left to right.
3. All .not. operators are evaluated.
4. All .and. operators are evaluated (from left to right).
5. All .or. operators are evaluated (from left to right).
6. All .eqv. and .neqv. operators are evaluated (from left to right).

If you do not remember all of the rules, as I have mentioned it before, you can always use () to define the precedence.

## Summary

• A logical expression results in either .true. or .false. literal.
• The precedence of operators is arithmetic operators > relational operators (==,/=,>,>=,<,<=) > .not > .and. > .or. > .eqv. and .neqv..
• You can change the precedence using ().

### Exercises

1. Write a formula to test a variable x to be $0 < x \leq 10$.
2. Write a formula to test three variables x, y, and z to be x=y=z.

Back to index.html.