Swift Operators

Swift's operators cover arithmetic, comparison, and logical operations, plus the nil-coalescing operator ?? for cleanly supplying a fallback when a value is missing.

Arithmetic Operators

Swift supports the standard arithmetic operators: + for addition, - for subtraction, * for multiplication, / for division, and % for remainder (modulo). These work on both Int and Double, though dividing two integers always produces an integer result, discarding any remainder.

Arithmetic in action

let a = 17
let b = 5

print(a + b)  // 22
print(a - b)  // 12
print(a * b)  // 85
print(a / b)  // 3, integer division drops the remainder
print(a % b)  // 2, the remainder
Note: Integer division truncates rather than rounds: 17 / 5 is 3, not 3.4. Convert to Double first if you need a fractional result.

Comparison Operators

Comparison operators evaluate to a Bool and are the backbone of every condition you write: == (equal to), != (not equal to), and the ordering operators <, >, <=, >=. They work on numbers, strings, and any type that conforms to the Equatable or Comparable protocols.

Comparisons

let x = 10
let y = 20

print(x == y)   // false
print(x != y)   // true
print(x < y)    // true
print(x >= 10)  // true
print("apple" < "banana") // true, alphabetical comparison

Logical Operators

Logical operators combine or invert Bool values: && (AND, true only if both sides are true), || (OR, true if either side is true), and ! (NOT, flips a Bool). Swift short-circuits && and ||, meaning the right-hand side is skipped entirely if the left-hand side already determines the result.

Combining conditions

let age = 25
let hasLicense = true

if age >= 18 && hasLicense {
    print("Can drive")
}

let isWeekend = false
let isHoliday = true
if isWeekend || isHoliday {
    print("No work today")
}

print(!isWeekend) // true

The Nil-Coalescing Operator ??

Optionals in Swift represent a value that might be missing, and the nil-coalescing operator ?? gives you a clean, one-line way to supply a default when that value turns out to be nil. a ?? b unwraps a if it holds a value, otherwise it evaluates and returns b.

Using ?? for defaults

let storedName: String? = nil
let displayName = storedName ?? "Guest"
print(displayName) // "Guest"

let cachedScore: Int? = 87
let finalScore = cachedScore ?? 0
print(finalScore) // 87, storedName had a value so the default was unused
  • Arithmetic: +, -, *, /, % — remember integer division truncates.
  • Comparison: ==, !=, <, >, <=, >= — always produce a Bool.
  • Logical: &&, ||, ! — combine or invert conditions, with short-circuit evaluation.
  • Nil-coalescing ??: unwraps an optional or falls back to a default value.

Operator Precedence Cheat Sheet

CategoryOperatorsExample
Arithmetic+ - * / %total = price * quantity
Comparison== != < > <= >=isValid = age >= 18
Logical&& || !canProceed = isValid && !isBlocked
Nil-coalescing??name = optionalName ?? "Unknown"
Note: You can chain ?? multiple times: a ?? b ?? c tries a first, falls back to b, and finally falls back to c if both are nil.

Exercise: Swift Operators

What does the % operator compute in Swift?