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
- Parentheses are always evaluated first.
*,/and%come next.+and-come last.- 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 * 3isn't5 * 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, so2 ^ 3prints1instead of 8.
Key takeaways
*,/and%run before+and-.- Equal levels run left to right.
- Parentheses override everything, and the innermost pair runs first.
