Swift Operators
Operators are symbols that perform actions on values. Swift uses operators to do math, compare values, and combine conditions. You already know many of them from everyday math.
Arithmetic Operators – The Math Ones
let a = 20
let b = 6
print(a + b) // 26 → Addition
print(a - b) // 14 → Subtraction
print(a * b) // 120 → Multiplication
print(a / b) // 3 → Division (whole number result)
print(a % b) // 2 → Remainder (modulo)
The % (modulo) operator gives you what is left after division. 20 divided by 6 is 3 with a remainder of 2.
The Remainder Operator in Real Life
┌──────────────────────────────────────────────┐
│ Sharing 20 chocolates among 6 friends │
│ │
│ Each friend gets: 20 / 6 = 3 chocolates │
│ Left over: 20 % 6 = 2 chocolates │
│ │
│ The modulo tells you what is left over. │
└──────────────────────────────────────────────┘
Assignment Operators
var score = 10
score += 5 // same as score = score + 5 → 15
score -= 3 // same as score = score - 3 → 12
score *= 2 // same as score = score * 2 → 24
score /= 4 // same as score = score / 4 → 6
score %= 4 // same as score = score % 4 → 2
These shorthand operators save typing. Instead of writing score = score + 5, you write score += 5.
Comparison Operators – Ask Yes or No Questions
let x = 10
let y = 20
print(x == y) // false → Are they equal?
print(x != y) // true → Are they different?
print(x > y) // false → Is x greater?
print(x < y) // true → Is x smaller?
print(x >= 10) // true → Is x 10 or more?
print(x <= 5) // false → Is x 5 or less?
Comparison operators always produce a Bool — either true or false. You use them to make decisions in your code.
Logical Operators – Combine Conditions
let hasTicket = true
let isAdult = false
print(hasTicket && isAdult) // false → Both must be true
print(hasTicket || isAdult) // true → At least one must be true
print(!hasTicket) // false → Flips the value
Logical Operators Diagram
┌──────────────────────────────────────────────────┐
│ AND (&&) – Both doors must be open to pass │
│ [Door A: Open] AND [Door B: Closed] = BLOCKED │
│ │
│ OR (||) – At least one door open to pass │
│ [Door A: Open] OR [Door B: Closed] = PASS │
│ │
│ NOT (!) – Flips the state │
│ NOT [Open] = [Closed] │
└──────────────────────────────────────────────────┘
Range Operators
// Closed range – includes both ends
let fullRange = 1...5 // 1, 2, 3, 4, 5
// Half-open range – excludes the last number
let partRange = 1..<5 // 1, 2, 3, 4
Range operators create sequences of numbers. They appear frequently in loops and array operations.
String Concatenation
let firstName = "Riya"
let lastName = "Sharma"
let fullName = firstName + " " + lastName
print(fullName) // Riya Sharma
The + operator joins two strings together. Adding a space between them creates proper formatting.
Ternary Operator – A One-Line Decision
let age = 18
let status = age >= 18 ? "Adult" : "Minor"
print(status) // Adult
The ternary operator checks a condition, then picks one of two values. The format is: condition ? valueIfTrue : valueIfFalse. It replaces a short if-else block in a single line.
