Skip to main content
Engineering LibreTexts

5.3: Order of Operations

  • Page ID
    54257
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Fortran follows the standard mathematical order of operations or precedence of operations. That is, multiplication and division are performed before addition and subtraction. Further, in accordance with mathematical standards, the exponentiation operation is performed before multiplication and division.

    The following table provides a partial summary of the basic Fortran 95/2003/2008 precedence levels:

    Precedence Level Operator Operation
    1st - unary -
    2nd ** exponentiation
    3rd * / multiplication and division
    4th + - addition and subtraction

    For operations of the same precedence level, the expression is evaluated left to right. Parentheses may be used to change the order of evaluation as necessary. For example, declaring the variables ans1, ans2, num1, num2, and num3.

    integer :: ans1, ans2, num1=20, num2=50, num3=10
    

    and calculating the ans1 and ans2, as follows:

    ans1 = num1 + num2 * num3
    ans2 = (num1 + num2) * num3
    

    will set to ans1 to 520 and ans2 to 700 (both integers).


    This page titled 5.3: Order of Operations is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?