Menu Close

Operator Precedence – FORTRAN

In Fortran, operator precedence determines the order in which operators are evaluated within an expression.

It helps to clarify the grouping and hierarchy of operators when multiple operators are present in a single expression.

The following is a list of operator precedence in Fortran, from highest to lowest:

  1. Parentheses: ( )
    • Parentheses are used to group expressions and override the default precedence.
  2. Exponentiation: **
    • Exponentiation has the highest precedence. For example, in the expression 2**3**2, the inner exponentiation is evaluated first, resulting in 2**9.
  3. Unary Operators:
    • Unary Plus: +
    • Unary Minus: -
    • Logical NOT: .NOT.
    • Bitwise NOT: NOT
  4. Multiplicative Operators:
    • Multiplication: *
    • Division: /
    • Integer Division: //
    • Modulus: MOD
  5. Additive Operators:
    • Addition: +
    • Subtraction: -
  6. Concatenation: //
    • Concatenation is used to combine strings.
  7. Relational Operators:
    • Less than: <
    • Greater than: >
    • Less than or equal to: <=
    • Greater than or equal to: >=
    • Equality: ==
    • Inequality: /=
  8. Logical Operators:
    • Logical AND: .AND.
    • Logical OR: .OR.
  9. Bitwise Operators:
    • Bitwise AND: AND
    • Bitwise OR: OR
    • Bitwise Exclusive OR (XOR): XOR

Note – Operator precedence can be modified using parentheses.

Expressions within parentheses are evaluated first, regardless of the default precedence.

Tip – It is good practice to use parentheses to clarify the intended order of evaluation, especially when complex expressions are involved.

It’s worth noting that Fortran provides strict operator precedence rules, and it is always recommended to use parentheses when in doubt or to improve code readability.

More Related Stuff