Skip to main content Skip to navigation

4. Operators

Operators in Fortran are used to manipulate and compare variables and constants.

1. Arithmetic operators

These can be applied to real numbers and integers. The operands can be integers, real numbers or a mixture of the two. If the operands for any of the above operators are two integers, the result will also be an integer; if at least one of the operands is a real number, the result will be a real number. The operators are summarised below.

addition +
subtraction -
multiplication *
division /
exponentiation **

Putting it into Practice:

In this example, write code so that it follows the code given below. This will give some examples of operators in action;
program getname
implicit none

print *, "The result of 22/4 is", 22/4
print *, "The result of 22.0/4.0 is", 22.0/4.0
print *, "The result of 15/4*4.0 is", 15/4*4.0

end program getname
Compile and run this code. The output should be:
       The result of 22/4 is 5
       The result of 22.0/4.0 is 5.500000 
       The result of 15/4*4.0 is 12.000000
The last line of output comes from the integer division being carried out first

Back to top

2. Comparison operators

To compare two variables or constants in F90, comparison operators are used. These operators return a value of 0 or 1, with 0 meaning that the comparison statement is false, and 1 meaning that it is true. The operators are summarised below - there are two equivalent syntaxes

equality .eq.   ==
inequality .ne.   /=
less than .lt.   <
greater than .gt.   >
less than or equal to .le.  
greater than or equal to .ge.  

Comparison operators are most useful when used in decision and control statements (more later).

One thing to watch out for: don't use these operators to directly compare two real numbers for equality. Because of how computers treat real numbers, instead define a tolerance, and check if the absolute difference is smaller than this tolerance, e.g.

real :: a,b
real, parameter  :: tol = 1.0e-06
logical :: same

! compare the value of a and b
same = (abs(a-b).lt.tol)

Back to top

3. Logical operators

These operators work on truth values and return another truth value. The operators are summarised below.

.not. a   .true. if a is false and vice versa
a .and. b   .true. if both a and b are .true.
a .or. b   .true. if either a or b are .true.
a .eqv. b   .true. if a and b are the same

Back to top

4. Character operators

Two character operations given here are the concatenation operator (//) and the substring operator. See examples below for usage.


Below are some examples of string operations
Expression Value
 string1 // string2 
 string2(2:3) // string1(1:2)  

Back to top

5. Operator precedence

When applying many operators together in one statement, certain operators will be evaluated first. The following list gives the order in which these operations are carried out under F90, in descending order of precedence:

  • exponentiation
  • multiplication and division
  • addition and subtraction

Back to top