Start learning free

LESSON 20 OF 415 · CALCULATIONS WITH NUMBERS

Operator Precedence in Swift: Order of Evaluation

Lesson Read the full explanation

TL ; DR:

  • When we string operations together, Swift must know which one to do first. This is called operator precedence.


  • * and / are performed before + and -:

  • print(1 + 2 * 3) // Prints 7
    print(6 - 4 / 2) // Prints 4
    print(5 * 4 - 2 / 2) // Prints 19
    

  • We can add () to force an operation to be performed first:

    print((1 + 2) * 3) // Prints 9
    print((6 - 4) / 2) // Prints 1
    print(5 * (4 - 2) / 2) // Prints 5
    


Full lesson:

When we string operations together, Swift must know which one to do first. This is called operator precedence.

This is the hierarchy from highest precedence to lowest precedence:

  • Parentheses are always respected
  • Multiplication, Division and Remainder
  • Addition and Subtraction
  • Left to right


First example:

print(1 + 2 * 3) // Output: 7

Multiplication is executed before addition, so:

1 + 2 * 3 = 1 + 6 = 7

Second example:

print((1 + 2) * 3) // Output: 9

What's inside parentheses is executed before multiplication, so:

(1 + 2) * 3 = 3 * 3 = 9

Third example:

print(6.0 / 4.0 * 3.0 + 5.0) // Output: 9.5

Multiplication and division come before addition. Also, multiplication and division have the same priority so they are executed from left to right:

6 / 4 * 3 + 5 = 1.5 * 3 + 5 = 4.5 + 5 = 9.5

Fourth example:

print(6 / ((4 + 2) / 3)) // Output: 3

Computer sees parentheses so it looks at it like this:

6 / ((4 + 2) / 3) = 6 / x, where x = (4 + 2) / 3

I have to compute x first. Parentheses are executed before division:
(4 + 2) / 3 = 6 / 3 = 2

I can replace x with 2:
6 / ((4 + 2) / 3) = 6 / 2 = 3


Assignment
Follow the Coding Tutorial and let's practice with operator precedence!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · CALCULATIONS WITH NUMBERS

    Operator Precedence in Swift, explained

    What precedence is

    When an expression has several operators, Swift has to decide which one runs first. The rule that decides is operator precedence. It is the same idea as in school arithmetic: multiplication and division bind tighter than addition and subtraction.

    The rules for arithmetic

    1. Parentheses are always evaluated first.
    2. *, / and % come next.
    3. + and - come last.
    4. Operators on the same level run from left to right.

    Swift has no exponent operator. Writing 2 ** 3 fails with cannot find operator '**' in scope. Powers come from a function such as pow in Foundation, which is a separate topic.

    Examples without parentheses

    print(1 + 2 * 3)       // 7
    print(6 - 4 / 2)       // 4
    print(5 * 4 - 2 / 2)   // 19
    

    In 1 + 2 * 3, the product 6 is computed first and 1 is added. In 6 - 4 / 2, the division gives 2 and the subtraction gives 4. In 5 * 4 - 2 / 2, both the multiplication (20) and the division (1) run before the subtraction, which gives 19.

    Forcing an order with parentheses

    print((1 + 2) * 3)       // 9
    print((6 - 4) / 2)       // 1
    print(5 * (4 - 2) / 2)   // 5
    

    The sum in (1 + 2) * 3 is computed first, so the result is 3 * 3. In the last line, the parentheses give 2, and then 5 * 2 / 2 runs from left to right: 10, then 5.

    Left to right on equal levels

    Multiplication and division share a level, so they run in the order you read them:

    print(6.0 / 4.0 * 3.0 + 5.0)   // 9.5
    

    Step by step: 6.0 / 4.0 is 1.5, times 3.0 is 4.5, plus 5.0 is 9.5.

    The same holds for subtraction, where the order changes the answer:

    print(10 - 4 - 3)     // 3
    print(10 - (4 - 3))   // 9
    print(100 / 10 / 5)   // 2
    print(100 / (10 / 5)) // 50
    

    Without parentheses, 10 - 4 - 3 is (10 - 4) - 3. Grouping the right-hand part changes it to 10 - 1.

    Nested parentheses

    The innermost group is evaluated first:

    print(6 / ((4 + 2) / 3))   // 3
    

    Here 4 + 2 is 6, then 6 / 3 is 2, and the outer division is 6 / 2, which is 3. With integers, each division drops its fraction, so the order matters there too: 8 / 3 * 3 is 6, while 8 * 3 / 3 is 8.

    The exercise

    The starter code is:

    print(2 + 3 * 3)
    

    It prints 11, because the multiplication runs first: 3 * 3 is 9, and 2 + 9 is 11. The task is to change the output to 15. To get 15 from a multiplication by 3, the sum 2 + 3 must be computed first, since 5 * 3 is 15. Wrapping that sum in parentheses forces it to run first.

    The solution in Swift

    print((2 + 3) * 3)
    

    The output is:

    15
    

    Once this is clear, the same habit applies to longer formulas. For the average of two values, a + b / 2 divides only b by 2; the correct form is (a + b) / 2. Whenever you mean "do this part first", write the parentheses.

    Common mistakes

    • Reading left to right only. 2 + 3 * 3 isn't 5 * 3. Only operators on the same level go left to right.
    • Parentheses in the wrong place. 2 + (3 * 3) is the same as the original expression and still gives 11, because the multiplication was already first.
    • Relying on memory in long expressions. Extra parentheses cost nothing and make the intent clear.
    • Expecting a power operator. ** doesn't exist, and ^ is not a power either. On integers it is bitwise exclusive-or, so 2 ^ 3 prints 1 instead of 8.

    Key takeaways

    • *, / and % run before + and -.
    • Equal levels run left to right.
    • Parentheses override everything, and the innermost pair runs first.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free