Values and types
Swift Operators
A guide to Swift operators, covering the assignment, arithmetic, compound assignment, comparison, range, and logical operators you use to work with values.
This tutorial belongs to the Swift series
Operators are the symbols we use to work with values: add them, compare them, combine them.
One way to group operators is by how many values they work on. Unary operators take one value, like the - in -20. Binary operators take two, like 1 + 1. There’s a single ternary operator, which takes three.
The more useful grouping is by what they do:
- assignment operator
- arithmetic operators
- compound assignment operators
- comparison operators
- range operators
- logical operators
There are also more advanced ones, like nil-coalescing, ternary conditional, overflow, bitwise, and pointwise operators. We’ll leave those for later.
Swift also lets you create your own operators, and define how existing operators work on the types you define.
Assignment operator
= assigns a value to a variable:
var age = 8
Or copies the value of one variable into another:
var age = 8
var another = age
Arithmetic operators
Swift has the usual binary arithmetic operators: +, -, *, / (division), and % (remainder):
1 + 1 // 2
2 - 1 // 1
2 * 2 // 4
4 / 2 // 2
4 % 3 // 1
4 % 2 // 0
Notice that / between two Int values gives an Int. 7 / 2 is 3, not 3.5. Use Double values when you need decimals.
- also works as a unary minus:
let hotTemperature = 20
let freezingTemperature = -20
And + joins two strings:
"Roger" + " is a good dog"
Compound assignment operators
These combine an arithmetic operator with assignment, so you can update a variable in place:
+=-=*=/=%=
Example:
var age = 8
age += 1 // age is now 9
age += 1 is a shorter way to write age = age + 1.
Comparison operators
Swift gives you six comparison operators:
==!=><>=<=
Each one compares two values and returns a Bool:
let a = 1
let b = 2
a == b // false
a != b // true
a > b // false
a <= b // true
Range operators
Range operators define a sequence of numbers, and you’ll meet them mostly in loops. ... is the closed range operator and includes both ends. ..< is the half-open range operator and leaves out the last value:
0...3 // 0, 1, 2, 3: 4 values
0..<3 // 0, 1, 2: 3 values
0...count // count + 1 values
0..<count // count values
The half-open form is the one you want for array indexes, because an array with count items has indexes from 0 to count - 1.
Here’s a range driving a loop:
let count = 3
for i in 0...count {
print(i) // 0, 1, 2, 3
}
Logical operators
Swift has three logical operators:
!, the unary NOT&&, the binary AND||, the binary OR
Sample usage:
let condition1 = true
let condition2 = false
!condition1 // false
condition1 && condition2 // false
condition1 || condition2 // true
You’ll mostly use them inside an if condition:
if condition1 && condition2 {
// if body
}
&& stops early: if the left side is false, Swift never evaluates the right side. Same for || when the left side is true. This is called short-circuit evaluation, and it’s handy when the right side is expensive or only makes sense after the left side passed.
Lesson completed