Back to index.html.
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.
Fortran has unique literals to express the logical status, true and false.
||the status of true|
||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
T for true and
F for false.
program bool print *,.true. print *,.false. end program bool
Relational (comparison) operators
There are some operators to compare two values and return
||Greater than or equal to||
||Less than or equal to||
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
.not. operator takes 1 expression, but the others take 2 expressions.
||Logical Not; returns the opposite value|
The expressions can be encircled with
program bool print *,10>9 .and. 10>=9 print *,(10>9).and.(10>=9) ! more readable print *,(10<5).or.(10>5) print *,.not.(10==5) end program bool
T T T T
The precedence of the Boolean operators is
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
== 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
.neqv., you can apply any logical operators (like
.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).
- First, all arithmetic operators (
**) are evaluadted.
- All relational operators (
<=) are evaluated from left to right.
.not.operators are evaluated.
.and.operators are evaluated (from left to right).
.or.operators are evaluated (from left to right).
.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.
- A logical expression results in either
- The precedence of operators is arithmetic operators > relational operators (
- You can change the precedence using
- Write a formula to test a variable
xto be \(0 < x \leq 10\).
- Write a formula to test three variables
Back to index.html.