Operators

Swift's arithmetic, comparison, and logical operators look familiar if you've used almost any C-family language — the interesting parts are a couple of operators Swift adds that most of those languages don't have.

Arithmetic and compound assignment

Swift main.swift
let a = 17
let b = 5

print(a + b, a - b, a * b, a / b, a % b)
Output
22 12 85 3 2

a / b is 3, not 3.4 — dividing two Int values in Swift always produces an Int, truncating any remainder, exactly like C's integer division. Make at least one operand a Double to get a fractional result.

Comparison operators

Swift main.swift
let x = 10
let y = 20

print(x == y, x != y, x < y, x >= y)
Output
false true true false

Logical operators

Swift main.swift
let isLoggedIn = true
let hasSubscription = false

print(isLoggedIn && hasSubscription)
print(isLoggedIn || hasSubscription)
print(!hasSubscription)
Output
false
true
true
A preview: Swift also has a nil-coalescing operator, ??, that provides a fallback value when something might be missing — let name = nickname ?? "Anonymous". It doesn't make sense to demonstrate properly yet, since it's built specifically for optionals, which is the very next lesson.