Chapter 24: Swift Arithmetic Operators

1. What are Arithmetic Operators?

Arithmetic operators are the mathematical symbols you use to perform calculations:

  • + addition
  • – subtraction
  • * multiplication
  • / division
  • % remainder (modulo)

These operators work with numeric types:

  • Int (whole numbers)
  • Double (decimals – most common for real-world math)
  • Float (less common)
  • Decimal (exact decimals – used for money)

2. Basic Arithmetic Operators – with examples

Swift

Important difference when using floating-point numbers:

Swift

Realistic example – price calculation

Swift

3. Compound Assignment Operators – very common shorthand

These combine an operation with assignment.

Swift

Real-life pattern – accumulating totals

Swift

4. Unary Minus Operator – changing sign

Swift

Very common use case – reversing direction

Swift

5. Increment / Decrement Operators – ++ and —

These exist in Swift, but they are very rarely used in modern code.

Swift

Modern & clearer way (what you’ll see in real code 2024–2026):

Swift

Why ++ and — are avoided in modern Swift:

  • They can make code harder to read
  • They behave differently depending on prefix/postfix
  • += 1 is clearer and safer

6. Arithmetic with Decimal – very important for money

Swift

Important note:

  • Never do money math with Double → floating-point errors
  • Always use Decimal for currency

7. Very Common Beginner Mistakes & Correct Habits

Mistake Wrong / Dangerous code Correct / Better habit Why?
Doing money math with Double total += 0.1 many times Use Decimal for prices Avoids 0.30000000000000004 bugs
Using / with Int and expecting decimals 10 / 3 → 3 10.0 / 3.0 or Double(10) / 3 Integer division truncates
Writing x++ in complex expressions array[x++] array[x]; x += 1 Much clearer, avoids bugs
Forgetting to convert types price * quantity where quantity is Int price * Decimal(quantity) Prevents type mismatch
Using % with negative numbers carelessly -10 % 3 Be aware: Swift gives positive remainder -10 % 3 == 2 in Swift

8. Quick Reference – Arithmetic Operators Cheat Sheet

Operator Meaning Example Result Most common real use
+ Addition a + b sum Totals, concatenating strings
Subtraction a – b difference Discounts, differences
* Multiplication price * quantity product Subtotal, scaling
/ Division total / count quotient Average, splitting amounts
% Remainder (modulo) value % 2 remainder Checking even/odd, cycling values
+= -= *= /= %= Compound assignment score += 100 update Accumulating scores, totals, counters
– (unary) Negation -value negative Reverse direction, debt

9. Small Practice – Try these now

  1. Calculate final price: base price = 2499.99 quantity = 2 discount = 20% GST = 18% → what is the final amount?
  2. Check if a number is divisible by 5 let number = 125 → use % and print “Divisible” or “Not divisible”
  3. Update a counter every time an item is added to cart var itemCount = 0 → add 3 items, then add 2 more

Paste your attempts if you want feedback!

What would you like to explore next?

  • Arithmetic with Decimal in more detail (money examples)
  • How to avoid floating-point surprises
  • Rounding numbers (round, ceil, floor)
  • Random numbers (Int.random(in:), Double.random(in:))
  • Or move to another group of operators (comparison, logical, range…)

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

You may also like...

Leave a Reply

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