Chapter 25: Swift Assignment Operators

1. What are Assignment Operators?

An assignment operator is used to assign a value to a variable or constant (or more generally: to a place in memory).

The most basic one is the single = sign:

Swift

But Swift has a whole family of assignment operators — the most important ones are the compound assignment operators.

2. The Basic Assignment Operator: =

Swift

Important rule (very important mindset):

  • Use let when the value will never change after the first assignment
  • Use var when the value will be replaced with a new value later

This is not just style — it’s one of the strongest safety features of Swift.

3. Compound Assignment Operators — the ones you use every day

These combine an arithmetic operation with assignment in one step.

Operator Meaning Equivalent to Most common real use case
+= Add and assign x = x + y Accumulating scores, totals, counters
-= Subtract and assign x = x – y Reducing lives, applying discounts
*= Multiply and assign x = x * y Scaling values, doubling, taxes
/= Divide and assign x = x / y Averaging, shrinking values
%= Remainder and assign x = x % y Cycling values, checking patterns

Live examples – try these right now

Swift

4. Real-life examples – how compound assignment is actually used

Example 1 – Shopping cart total

Swift

Example 2 – Game score & lives

Swift

Example 3 – Progress bar / percentage

Swift

Example 4 – Cycling / wrapping values (using %=)

Swift

5. Very Common Beginner Mistakes & Correct Habits

Mistake Wrong / Risky code Correct / Better habit Why?
Using = instead of += in loops total = total + price every time total += price Shorter, clearer, less typing mistakes
Trying to use += on let let count = 0; count += 1 Use var if value must change let cannot be reassigned
Doing money math with += on Double total += 0.1 many times Use Decimal for money Avoids floating-point rounding errors
Writing x =+ 5 (wrong order) score =+ 100 score += 100 =+ is not valid syntax
Forgetting to declare variable first count += 1 without previous declaration var count = 0; count += 1 Must exist before modifying

6. Quick Reference – Assignment Operators Cheat Sheet

Operator Meaning Equivalent longer form Most common real-world use case
= Simple assignment Initial value, one-time setting
+= Add and assign x = x + y Accumulating totals, scores, counters
-= Subtract and assign x = x – y Decreasing lives, applying discounts
*= Multiply and assign x = x * y Doubling values, calculating tax/growth
/= Divide and assign x = x / y Averaging, shrinking progress bars
%= Remainder and assign x = x % y Cycling pages, checking even/odd, wrapping

7. Small Practice – Try these right now

Copy and complete these:

Swift

Would you like to go deeper into any part?

  • More real examples with Decimal (money, invoices, discounts)
  • How to combine arithmetic + comparison + assignment
  • Using arithmetic operators in loops and animations
  • Common patterns in games, forms, shopping apps
  • Or move to another group of operators (comparison, logical, range…)

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

You may also like...

Leave a Reply

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