Arithmetic and Compound Assignment Operators
Use +, -, *, /, % and compound forms safely.
Arithmetic Operators
Swift provides the four standard arithmetic operators for all number types: + (addition), - (subtraction), * (multiplication), and / (division). They work on Int, Double, and Float.
let sum = 7 + 3
let diff = 10 - 4
let product = 6 * 5
print(sum, diff, product)Addition and Subtraction
Addition and subtraction behave exactly as in math. The result type matches the operand types: two Int values produce an Int.
let apples = 5
let eaten = 2
let remaining = apples - eaten
print("Remaining apples:", remaining)All lessons in this course
- Arithmetic and Compound Assignment Operators
- Comparison and Identity Operators
- Logical Operators and Short-Circuiting
- Bitwise and Overflow Operators