Arithmetic
What you'll learn:
- predict the result of dividing whole numbers and of the remainder, negative numbers included;
- explain what happens when a number does not fit its type, and what
&+is for; - compare values and combine conditions with
&&,||and!; - check whether a number falls in a range, and pick one of two values with the
? :operator.

Swift counts like a calculator, but this calculator has rules of its own. It remembers the type of every number, whole or fractional, and the type decides the answer.
Adding, subtracting and multiplying whole numbers works the way it did at school. Division is different. Picture sharing 17 sweets among five kids. You can't cut the sweets, so each kid gets a whole number of them, and the extras stay with you. That is how Swift divides whole numbers:
let sweets = 17
let kids = 5
print(sweets / kids) // 3
print(sweets % kids) // 2Let's see what the program printed:
sweets / kidsprints3: seventeen sweets among five kids is three each.sweets % kidsprints2: two sweets stay with you.
A pocket calculator would show 3.4, but Swift showed 3. The sweets picture works well for positive numbers. Negative ones follow a rule of their own, and we'll get to it in the section on the remainder.
Five arithmetic operators
A symbol for an action, such as +, is called an operator. Swift has five of them for numbers:
+adds:7 + 2is9;-subtracts:7 - 2is5;*multiplies:7 * 2is14;/divides:7 / 2is3;%gives the remainder of a division:7 % 2is1.
The order of operations is the same as in math: *, / and % run before + and -. Parentheses change the order: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20.
Dividing whole numbers
When both numbers have type Int, the result is an Int too, and an Int has no fractional part. Such division is called integer division: the fraction of the result is dropped, not rounded.
17 / 5is3, even though on paper it comes to3.4.19 / 5is3too, even though3.8is closer to four.-17 / 5is-3, not-4: a negative result drops its fraction as well.
Integer division always moves the result toward zero. In the previous chapter Int(2.9) turned into 2 and Int(-2.9) into -2 the same way.
The remainder and its sign
The % operator gives what is left after integer division. The quotient and the remainder are linked: the quotient times the divisor, plus the remainder, gives back the original number. For the sweets that is 3 * 5 + 2, which is 17.
This link gives the rule for negative numbers: the remainder has the same sign as the dividend, the number to the left of %.
print(-17 / 5, -17 % 5) // -3 -2
print(17 / -5, 17 % -5) // -3 2Let's go through both lines:
- In the first line the quotient is
-3, and-3 * 5is-15. Getting to-17takes another-2, and that is the remainder. - In the second line the dividend
17is positive, so the remainder2is positive too, even though the divisor is negative.
Interviewers like to ask this: what sign does the remainder have when the dividend is negative? In math the remainder is usually taken as non-negative, while in Swift it takes the sign of the dividend.
The % operator works only on whole numbers. If you apply it to a Double, the compiler reports an error.
A fractional result
A fraction comes from dividing numbers of type Double. There are two ways to get one.
A fractional number in the code
A number with a dot, such as 7.0, is already fractional. Swift reads the 2 next to it as a Double too, and 7.0 / 2 gives 3.5.
Converting constants
A constant already has a type, and that type never changes on its own. So both numbers are converted to Double before dividing: Double(sweets) / Double(kids) gives 3.4.
Compound assignment
score = score + 5 comes up so often that it has a shorthand: score += 5. Such operators are called compound assignment.
var score = 10
score += 5
score -= 3
score *= 2
print(score) // 24Let's go line by line:
score += 5adds 5:scorenow holds15.score -= 3subtracts 3: now it holds12.score *= 2multiplies by 2: now it holds24.print(score)prints24.
Compound assignment changes the variable, so it works only with var.
The score++ you may know from other languages does not exist in Swift:
error: cannot find operator '++' in scope; did you mean '+= 1'?The compiler suggests the replacement itself: score += 1.
Common mistake
The result of dividing two Int values is converted to Double in the hope of getting a fraction:
let total = 7
let parts = 2
let wrong = Double(total / parts) // ✗
let right = Double(total) / Double(parts) // ✓
print(wrong, right) // 3.0 3.5There is no compile error, but wrong ends up holding 3.0. First total / parts runs, which is integer division, and only its result 3 becomes a Double. Convert the numbers to Double before dividing, as on the line with right.
Key points
- Dividing two
Intvalues drops the fraction:17 / 5is3, and-17 / 5is-3. %gives the remainder, and its sign matches the dividend's:-17 % 5is-2.- To get a fraction, convert the numbers to
Doublebefore dividing, not the result. +=,-=and*=change a variable in place, and Swift has no++operator.