Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 39 ยท Language Basics

Operators and Expressions

Learn Fortran's arithmetic, relational, logical and character operators, precedence rules, integer division, exponentiation and mixed-type expressions with examples.

An expression combines values, variables, function calls and operators into a new value. Fortran has four groups of operators: arithmetic, relational (comparisons), logical, and the character concatenation operator. This lesson covers all four, the precedence rules that decide evaluation order, and the traps with integer division and mixed types.

Arithmetic operators

Operator Meaning Example Result
+ addition 5 + 2 7
- subtraction 5 - 2 3
* multiplication 5 * 2 10
/ division 5.0 / 2.0 2.5
** exponentiation 5 ** 2 25

There is no % operator and no ++. For the remainder use the mod function, and use x = x + 1 to increment.

FORTRAN
program arithmetic
  implicit none
  integer :: a, b
  real :: x, y

  a = 17
  b = 5
  print *, a + b, a - b, a * b       ! 22 12 85
  print *, a / b, mod(a, b)          ! 3 2
  print *, a ** 2                    ! 289

  x = 17.0
  y = 5.0
  print *, x / y                     ! about 3.4
  print *, x ** 0.5                  ! about 4.123
end program arithmetic

The last two results are only approximate in single precision; the digits printed vary by compiler (see kinds and precision).

Integer division

When both operands of / are integers, the result is an integer and the fraction is truncated toward zero:

FORTRAN
print *, 7 / 2      ! 3
print *, -7 / 2     ! -3
print *, 7 / 2.0    ! 3.5   (one operand is real)

This is the single most common beginner surprise. If you want a real result from two integer variables, convert one: real(a) / real(b). The related function mod(a, b) has the sign of a, while modulo(a, b) has the sign of b: mod(-7, 3) is -1, modulo(-7, 3) is 2.

Python Stand-in (Python): integer division, mod and modulo

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exponentiation

** raises to a power, and it is right-associative: 2 ** 3 ** 2 is 2 ** (3 ** 2), which is 512, not 64. Integer powers are exact for integers (2 ** 10 is 1024). A negative integer exponent on an integer base gives an integer result, which is almost never what you want: 2 ** (-1) is 0 (it means 1 / 2, truncated). Use a real base: 2.0 ** (-1) is 0.5.

Prefer an integer exponent when you can: x ** 2 is computed by a single multiplication, while x ** 2.0 may call a slower general power routine.

Precedence

From highest to lowest, operators are evaluated in this order:

  1. **
  2. * and /
  3. unary + and -, then binary + and -
  4. // (character concatenation)
  5. relational operators (==, /=, <, <=, >, >=)
  6. .not.
  7. .and.
  8. .or.
  9. .eqv. and .neqv.

Operators of equal precedence group left to right, except **. Parentheses override everything, and you should use them freely: a + b * c is clear, but -a ** 2 is -(a ** 2), which surprises people who expect (-a) ** 2.

Python Stand-in (Python): precedence of the power operator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Relational operators

Comparisons produce a logical. Modern Fortran accepts both the symbols and the old dotted forms:

Meaning Symbol Old form
equal == .eq.
not equal /= .ne.
less than < .lt.
less or equal <= .le.
greater than > .gt.
greater or equal >= .ge.

Note that not-equal is /=, not !=. Comparing real numbers with == is unreliable because of rounding: use a tolerance, abs(a - b) < 1.0e-6.

Logical operators

Operator Meaning
.not. negation
.and. both true
.or. at least one true
.eqv. equivalent (both true or both false)
.neqv. not equivalent (exclusive or)
FORTRAN
logical :: adult, member, discount
adult  = age >= 18
member = .true.
discount = adult .and. (member .or. total > 100.0)

Fortran does not guarantee short-circuit evaluation: in a .and. b the compiler may evaluate b even if a is false. So never write i <= n .and. x(i) > 0.0 hoping the second part is skipped when i > n; use a nested if instead (see the if statement lesson).

Concatenation and mixed expressions

The // operator joins strings: "Hello, " // "World" gives Hello, World. (More in characters and strings.)

When operands of different numeric types meet, the "lower" type is converted to the "higher": integer to real to complex, and a smaller kind to a larger kind. Conversion happens per operation, so 1 / 2 + 0.5 is 0 + 0.5 = 0.5, not 1.0: the division happens first, between two integers.

Python Stand-in (Python): evaluation order with mixed types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the value of 2 ** 3 ** 2?

    ** is right-associative, so it is 2 ** (3 ** 2) = 2 ** 9 = 512.
  2. 2. Which operator means not equal?

    Fortran uses /= (or the old .ne.) for not equal.
  3. 3. What does mod(-7, 3) return?

    mod has the sign of its first argument: -7 = (-2) * 3 + (-1). modulo(-7, 3) would be 2.
  4. 4. Is Fortran guaranteed to short-circuit .and. and .or.?

    The standard does not guarantee short-circuiting; use nested if statements when the second test is only safe after the first.

Summary

  • Arithmetic uses + - * / **; there is no %, use mod or modulo.
  • Integer division truncates toward zero; ** is right-associative.
  • Relational operators are == /= < <= > >=; logical ones are .not. .and. .or. .eqv. .neqv..
  • // concatenates strings.
  • Operands are converted to the highest type, one operation at a time; no short-circuit guarantee for logicals.

Next: Characters and Strings