Swift Operators
Operators are symbols that perform actions on values. They are the verbs of programming — they tell Swift to add, compare, combine, or check values. Swift organizes operators into clear categories.
Arithmetic Operators
These operators do basic math.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3 (integer) |
| % | Remainder | 10 % 3 | 1 |
let a = 10
let b = 3
print(a + b) // 13
print(a - b) // 7
print(a * b) // 30
print(a / b) // 3 (no decimal for Int division)
print(a % b) // 1Division With Doubles
Integer division drops the decimal. Use Double to keep it.
let x: Double = 10
let y: Double = 3
print(x / y) // 3.3333333333333335Assignment Operators
These operators set or update a variable's value.
var count = 0
count = 5 // Assign: count is now 5
count += 3 // Add and assign: count is now 8
count -= 2 // Subtract and assign: count is now 6
count *= 4 // Multiply and assign: count is now 24
count /= 6 // Divide and assign: count is now 4
count %= 3 // Remainder and assign: count is now 1Comparison Operators
These operators compare two values and return true or false.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 7 > 3 | true |
| < | Less than | 2 < 9 | true |
| >= | Greater or equal | 5 >= 5 | true |
| <= | Less or equal | 3 <= 4 | true |
let age = 18
print(age >= 18) // true — can vote
print(age == 21) // falseLogical Operators
Logical operators combine Bool conditions.
AND — Both must be true
let hasTicket = true
let hasID = true
if hasTicket && hasID {
print("Entry allowed")
}
// Output: Entry allowedOR — At least one must be true
let isMember = false
let hasCoupon = true
if isMember || hasCoupon {
print("Discount applied")
}
// Output: Discount appliedNOT — Flips the condition
let isDoorLocked = false
if !isDoorLocked {
print("Door is open")
}
// Output: Door is openDiagram: Logical Operators Truth Table
A B A && B A || B !A ----- ----- ------ ------ --- true true true true false true false false true false false true false true true false false false false true
Range Operators
Range operators create sequences of values. They appear often in loops and switch statements.
Closed Range — Includes Both Ends
for i in 1...5 {
print(i)
}
// Output: 1 2 3 4 5Half-Open Range — Excludes the Upper End
for i in 0..<3 {
print(i)
}
// Output: 0 1 2Ternary Operator
The ternary operator is a compact way to write a simple if/else decision.
let temperature = 30
let weather = temperature > 25 ? "Hot" : "Cool"
print(weather) // Output: HotRead it as: "If temperature is greater than 25, weather is Hot. Otherwise, weather is Cool."
Nil Coalescing Operator
The ?? operator provides a default value when an optional is empty (nil). You will learn more about optionals in a later topic.
var username: String? = nil
let displayName = username ?? "Guest"
print(displayName) // Output: GuestSummary
Swift operators cover arithmetic (+ - * / %), assignment (= += -=), comparison (== != > <), logical (&& || !), range (... ..<), ternary (? :), and nil coalescing (??). Each operator works on specific types and returns a predictable result.
