Operators in Swift
Learn arithmetic, comparison, logical, and Swift-specific operators like nil-coalescing, ranges, and overflow operators with Docker-ready examples
Operators are the symbols that let you compute, compare, and combine values. Swift inherits the familiar C-family operators but layers its safety-first philosophy on top of them: operators are strongly typed, will not silently coerce between number types, and trap on overflow by default rather than wrapping around quietly.
Because Swift is a multi-paradigm language with static, strong, inferred typing, the type of an expression is determined at compile time. An operator like + works on Int and on Double, but you cannot add an Int to a Double without converting one of them first — the compiler refuses to guess. This tutorial walks through arithmetic, comparison, and logical operators, then covers the operators that make Swift distinct: compound assignment, the ternary conditional, the nil-coalescing operator (??), range operators, overflow operators, and identity operators.
Everything below runs as a plain .swift script, so you can test each example with a single Docker command.
Arithmetic Operators
Swift provides the standard arithmetic operators. Note that integer division truncates toward zero, and there is no built-in exponentiation operator (you would use pow from Foundation for that).
Create a file named operators_arithmetic.swift:
| |
Because a and b are both Int, a / b performs integer division and discards the fractional part. Switching to Double literals (17.0, 5.0) gives a true floating-point result.
Comparison and Logical Operators
Comparison operators return a Bool, and the logical operators (&&, ||, !) combine those booleans. Swift’s && and || short-circuit, meaning the right-hand side is only evaluated if needed.
Create a file named operators_logical.swift:
| |
The ! operator inverts a boolean, so !isRaining is true when isRaining is false.
Assignment, Ternary, Nil-Coalescing, and Ranges
This example collects the operators that give Swift its expressive, concise feel. Compound assignment operators (+=, -=, *=) update a variable in place. The ternary operator (condition ? a : b) chooses between two values. The nil-coalescing operator (??) supplies a default when an optional is nil — a direct expression of Swift’s optionals safety model. Range operators build sequences for iteration.
Create a file named operators_swift_specific.swift:
| |
The closed range 1...3 includes both endpoints, while the half-open range 0..<3 excludes the upper bound — a distinction that maps cleanly onto zero-based array indices.
Precedence, Overflow, and Identity
Operators follow precedence rules (* binds tighter than +), and parentheses override them. Swift also adds overflow operators (&+, &-, &*) that intentionally wrap around instead of trapping, which is useful for low-level bit manipulation. The identity operators (===, !==) test whether two references point to the same class instance, distinct from == which tests value equality.
Create a file named operators_precedence.swift:
| |
Without the overflow operator, maxByte + 1 would crash at runtime because 255 is the maximum value of a UInt8. The &+ operator opts into wraparound, producing 0.
Running with Docker
Run each example with the official Swift image — no local toolchain required.
| |
Expected Output
operators_arithmetic.swift:
Sum: 22
Difference: 12
Product: 85
Quotient: 3
Remainder: 2
Float quotient: 3.4
Negation: -17
operators_logical.swift:
Above 18? true
Equal 22? true
Not 30? true
Nice weather: true
Stay inside: false
operators_swift_specific.swift:
Hello, Swift!
Score: 150
Category: Adult
Name: Anonymous
Closed range: 1
Closed range: 2
Closed range: 3
Half-open range: 0
Half-open range: 1
Half-open range: 2
operators_precedence.swift:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
255 &+ 1 = 0
Same instance: true
Key Concepts
- Strong typing, no implicit coercion — Swift will not mix
IntandDoublein an arithmetic expression; you must convert explicitly, which prevents silent precision bugs. - Integer division truncates —
17 / 5yields3; use floating-point operands when you need the fractional result. - Overflow traps by default — ordinary arithmetic crashes on overflow for safety; the opt-in
&+,&-,&*operators wrap around when you genuinely want that behavior. - Nil-coalescing (
??) — a concise, safe way to provide a fallback for optionals, central to Swift’s null-safety design. - Ternary operator —
condition ? a : bchooses a value inline, keeping simple branching expressive. - Range operators —
...is inclusive of both bounds,..<excludes the upper bound, mapping naturally to array indices and loops. ==vs===— value equality compares contents, while identity (===) checks whether two references are the same class instance.- Precedence and short-circuiting —
*binds tighter than+, parentheses override grouping, and&&/||skip evaluating the right side when the result is already determined.
Comments
Loading comments...
Leave a Comment