Swift Arithmetic Operators
Mathematical operations in Swift programming
🧮 What are Arithmetic Operators?
Arithmetic operators perform mathematical calculations on numbers. Swift provides standard operators like addition (+), subtraction (-), multiplication (*), division (/), and remainder (%) for performing math operations in your code.
let sum = 10 + 5 // Addition: 15
let difference = 10 - 3 // Subtraction: 7
let product = 4 * 6 // Multiplication: 24
Basic Arithmetic Operators
Addition (+)
Adds two numbers together
let sum = 15 + 25 // 40
let total = 3.5 + 2.1 // 5.6
Subtraction (-)
Subtracts one number from another
let diff = 20 - 8 // 12
let result = 10.5 - 3.2 // 7.3
Multiplication (*)
Multiplies two numbers
let product = 6 * 7 // 42
let area = 4.5 * 2.0 // 9.0
Division (/)
Divides one number by another
let quotient = 15 / 3 // 5
let decimal = 7.0 / 2.0 // 3.5
🔹 Basic Arithmetic Operations
Perform fundamental math operations with Swift:
// Addition
let a = 10
let b = 5
let sum = a + b // 15
// Subtraction
let difference = a - b // 5
// Multiplication
let product = a * b // 50
// Division
let quotient = a / b // 2
// Working with decimals
let price1 = 19.99
let price2 = 24.50
let totalPrice = price1 + price2 // 44.49
print("Sum: \(sum)")
print("Product: \(product)")
print("Total price: $\(totalPrice)")
Output:
Sum: 15
Product: 50
Total price: $44.49
🔹 Remainder Operator (%)
The remainder operator returns what's left after division:
// Remainder (modulo) operator
let remainder1 = 10 % 3 // 1 (10 ÷ 3 = 3 remainder 1)
let remainder2 = 15 % 4 // 3 (15 ÷ 4 = 3 remainder 3)
let remainder3 = 20 % 5 // 0 (20 ÷ 5 = 4 remainder 0)
// Checking if a number is even or odd
let number = 17
let isEven = number % 2 == 0
let isOdd = number % 2 == 1
// Practical example: cycling through values
let items = ["Red", "Green", "Blue"]
let index = 7
let selectedItem = items[index % items.count] // "Green" (7 % 3 = 1)
print("17 % 2 = \(number % 2)")
print("Is 17 even? \(isEven)")
print("Selected item: \(selectedItem)")
Output:
17 % 2 = 1
Is 17 even? false
Selected item: Green
🔹 Unary Minus Operator
The unary minus operator changes the sign of a number:
// Unary minus operator
let positiveNumber = 42
let negativeNumber = -positiveNumber // -42
// Toggling signs
var temperature = 25
temperature = -temperature // -25
// Using with expressions
let x = 10
let y = 5
let result = -(x + y) // -15
// Double negative
let originalValue = -30
let positiveValue = -originalValue // 30
print("Negative number: \(negativeNumber)")
print("Temperature: \(temperature)°C")
print("Result: \(result)")
print("Positive value: \(positiveValue)")
Output:
Negative number: -42
Temperature: -25°C
Result: -15
Positive value: 30
🔹 Compound Assignment Operators
Combine arithmetic operations with assignment:
// Compound assignment operators
var score = 100
// Addition assignment
score += 25 // score = score + 25 (now 125)
// Subtraction assignment
score -= 10 // score = score - 10 (now 115)
// Multiplication assignment
score *= 2 // score = score * 2 (now 230)
// Division assignment
score /= 5 // score = score / 5 (now 46)
// Remainder assignment
score %= 10 // score = score % 10 (now 6)
// Working with strings
var message = "Hello"
message += " World" // "Hello World"
message += "!" // "Hello World!"
print("Final score: \(score)")
print("Message: \(message)")
Output:
Final score: 6
Message: Hello World!
🔹 Operator Precedence
Swift follows mathematical order of operations:
Order of Operations (highest to lowest):
- Parentheses: ( )
- Unary operators: -, +
- Multiplication, Division, Remainder: *, /, %
- Addition, Subtraction: +, -
// Operator precedence examples
let result1 = 2 + 3 * 4 // 14 (not 20)
let result2 = (2 + 3) * 4 // 20
let result3 = 10 - 6 / 2 // 7 (not 2)
let result4 = (10 - 6) / 2 // 2
// Complex expression
let a = 5
let b = 3
let c = 2
let complex = a + b * c - 4 // 5 + 6 - 4 = 7
// Using parentheses for clarity
let clear = (a + b) * (c - 1) // (5 + 3) * (2 - 1) = 8
print("2 + 3 * 4 = \(result1)")
print("(2 + 3) * 4 = \(result2)")
print("Complex result: \(complex)")
print("Clear result: \(clear)")
Output:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Complex result: 7
Clear result: 8