Swift Operators
Operators are symbols that work on values: + adds, == compares, && combines conditions. Swift's operators look like those in C or JavaScript, with a few safety improvements.
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
addition | 7 + 2 |
9 |
- |
subtraction | 7 - 2 |
5 |
* |
multiplication | 7 * 2 |
14 |
/ |
division | 7 / 2 |
3 (integer division) |
% |
remainder | 7 % 2 |
1 |
let a = 17
let b = 5
print(a + b, a - b, a * b)
print(a / b) // whole part only
print(a % b) // what is left over
print(Double(a) / Double(b))
print(-a % b) // the sign follows the left operand22 12 85
3
2
3.4
-2
Note: Swift has no
++or--operators. Writecount += 1instead.
Assignment and compound assignment
var balance = 100
balance += 50 // balance = balance + 50
balance -= 30
balance *= 2
balance /= 4
print(balance)
var message = "Hello"
message += ", Swift"
print(message)60
Hello, Swift
Unlike C, an assignment does not return a value, so the classic bug if x = 5 { … } is a compile error.
Comparison operators
Comparisons produce a Bool:
| Operator | Meaning |
|---|---|
== / != |
equal / not equal |
< / > |
less / greater than |
<= / >= |
less or equal / greater or equal |
let score = 72
print(score >= 50)
print(score == 100)
print("apple" < "banana") // strings compare alphabetically
print((1, "zebra") < (2, "apple")) // tuples compare element by elementtrue
false
true
true
Logical operators
| Operator | Name | True when… |
|---|---|---|
!a |
NOT | a is false |
a && b |
AND | both are true |
a || b |
OR | at least one is true |
let hasTicket = true
let age = 15
let withParent = true
let canEnter = hasTicket && (age >= 16 || withParent)
print(canEnter)
print(!canEnter)true
false
&& and || are short-circuiting: the right side is not evaluated if the left side already decides the result.
Range operators
Ranges are used everywhere — in loops, switch cases and to slice arrays:
| Operator | Name | Contains |
|---|---|---|
1...5 |
closed range | 1, 2, 3, 4, 5 |
1..<5 |
half-open range | 1, 2, 3, 4 |
2... |
one-sided | 2 and everything after |
..<3 |
one-sided | everything before 3 |
let colors = ["red", "green", "blue", "yellow", "purple"]
print(colors[1...3])
print(colors[..<2])
print(colors[3...])
print((1...10).contains(7))["green", "blue", "yellow"]
["red", "green"]
["yellow", "purple"]
true
Ternary and nil-coalescing operators
let temperature = 28
let advice = temperature > 25 ? "Wear a T-shirt" : "Take a jacket"
print(advice)
let nickname: String? = nil
print(nickname ?? "Guest")Wear a T-shirt
Guest
Operator precedence
*, / and % bind more tightly than + and -; comparisons come next, then &&, then ||. When in doubt, use parentheses — they make the intent obvious:
print(2 + 3 * 4)
print((2 + 3) * 4)
print(10 - 4 - 3)14
20
3
Test yourself
What is 14 % 4?
- 3
- 2
- 3.5
- 0
Which numbers does 0..<3 contain?
- 0, 1, 2, 3
- 0, 1, 2
- 1, 2, 3
- 1, 2
How do you increase count by one in Swift?
- count++
- ++count
- count += 1
- inc(count)