Chapter 28: Swift Operator Precedence

1. What is Operator Precedence?

When you write an expression with multiple operators, Swift has to decide which one to do first.

Operator precedence = the set of rules that decides the order of operations.

Example most people recognize from school math:

Swift

What’s the result?

  • If we do left-to-right: 2 + 3 = 5 → 5 * 4 = 20
  • But the correct answer is 14 — because multiplication has higher precedence than addition

Swift follows very similar rules to math, but with more operators and some Swift-specific twists.

2. The Most Important Precedence Levels (the ones you meet every day)

Here are the precedence groups you will actually use — ordered from highest to lowest:

Priority Operators Meaning / Category Associativity Example Result
Highest ! ~ (unary minus, logical NOT, bitwise NOT) Unary operators Right -5 + 3 -2
Very high * / % &* &/ &% Multiplication, division, remainder Left 10 + 6 * 2 22
High + – &+ &- Addition, subtraction Left 2 + 3 * 4 14
Medium ..< … Range operators 1…5
Medium < <= > >= is as? as! Comparison & type casting age > 18 && hasLicense
Low == != === !== ~= Equality & identity a == b && c != d
Lower && Logical AND Left true && false
Lowest Logical OR Left
Very low ?? Nil-coalescing Right a ?? b ?? c
Lowest =, +=, -=, *=, /=, %=, … Assignment operators Right a = b + c

Key rules to remember:

  1. Higher precedence operators are evaluated first
  2. When precedence is the same → left-to-right (except assignment and ?? which are right-to-left)
  3. You can always force order using parentheses ()

3. Step-by-step examples – let’s calculate together

Example 1 – Classic math confusion

Swift

Correct way if you want 30:

Swift

Example 2 – Logical operators + comparison

Swift

What actually happens?

Because && has higher precedence than ||, it is evaluated first:

Swift

So:

  • hasConsent && !isRestricted → true && true → true
  • age >= 18 → false
  • false || true → true

If you wanted different grouping, use parentheses:

Swift

Example 3 – Assignment & arithmetic

Swift

If you wanted something else:

Swift

4. Very Common Real-Life Examples

Example 1 – Discount & tax calculation

Swift

Safer & clearer with parentheses:

Swift

Example 2 – Form validation (very common)

Swift

Here && chains are evaluated left to right — if any condition fails, the rest are skipped (short-circuit).

5. The Most Dangerous Beginner Mistake

Forgetting precedence between && and ||

Swift

Always use parentheses when mixing && and ||

Swift

6. Quick Reference – Precedence Summary (the operators you use most)

Priority Operators you meet often Evaluated first? Left-to-right or right-to-left?
Highest ! - (unary) Yes Right-to-left
High * / % Yes Left-to-right
Medium + - Left-to-right
Medium > < >= <= == !=
Low && Left-to-right
Lowest `
Lowest ?? Right-to-left
Lowest = += -= *= /= %= Right-to-left

7. Golden Rules for Writing Safe & Readable Code

  1. When in doubt → use parentheses(a + b) * c is clearer than a + b * c
  2. Never rely on precedence when mixing && and || Always group with ()
  3. Short conditions are usually clearer than one giant expression

Bad:

Swift

Better:

Swift

8. Small Practice – Predict the result

Try to guess before running:

Swift

What would you like to explore next?

  • More complex expressions with && / || / ?? / ranges
  • Operator precedence in switch and guard statements
  • How precedence interacts with optional chaining and nil-coalescing
  • Writing readable conditions in real forms / validation
  • Or move to another topic (control flow, functions, optionals…)

Just tell me — we’ll keep going in the same patient, detailed, teacher-like style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *