Fortran Expressions

An expression combines values, operators and function calls into one value. Fortran expressions read like mathematics: a = sqrt(b**2 + c**2). This lesson fixes the precedence rules, the dotted logical operators, and the intrinsic functions every program reaches for — including the array-aware behaviour that makes Fortran expressions special.

Operator precedence

Precedence is the mathematical hierarchy you already know, with exponents binding tighter than multiplication. Parentheses override everything; when in doubt, add them — the cost is zero and the reader never has to guess. The full order, from tightest to loosest:

PrecedenceOperatorsMeaning
1 (tightest)**exponentiation (right-associative)
2* /multiplication, division
3+ -addition, subtraction
4== /= < <= > >=relational comparisons
5.not.logical negation
6.and.logical conjunction
7.or.logical disjunction
8 (loosest).eqv. .neqv.logical equivalence

Beware the exponent trap: -2**2 parses as -(2**2) and equals -4, because exponentiation binds tighter than the unary minus. Write (-2)**2 for 4.

Arithmetic details

Integer division truncates toward zero: 7/3 is 2, -7/3 is -2. Mixing an integer with a real promotes to real, but only after the integer operation evaluates — 1/2*3.0 is 0.0, not 1.5. Modern Fortran 2023 added division that keeps the fractional part (7./3. style), and the safe habit stands: put a decimal point (and a kind suffix) on at least one operand of every real division.