The operators
Swift calculates with the usual operators. They work on numbers written directly in the code and on constants and variables that hold numbers.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | addition | 2 + 6 | 8 |
- | subtraction | 10 - 7 | 3 |
* | multiplication | 3 * 3 | 9 |
/ | division | 6 / 2 | 3 |
% | remainder | 7 % 3 | 1 |
With variables, Swift substitutes the stored values:
let num = 6
print(num + 5)
print(10 - num)
print(3 * num)
print(num / 2)
11
4
18
3
A calculation does not change the variables in it. num + 5 produces a new value and num is still 6. Only an assignment, such as num = num + 5, stores a result.
Integer division
When both operands are Int, / gives an Int and discards the fractional part. It truncates toward zero, so the result is not rounded:
print(20 / 3)
print(-7 / 2)
print(7 % 3, -7 % 3)
6
-3
1 -1
The remainder % goes with integer division: 20 / 3 is 6 and 20 % 3 is 2, because 6 * 3 + 2 is 20. A nonzero remainder takes the sign of the left operand, so -7 % 3 is -1.
For a fractional result, at least one side must be a Double. Swift does not mix Int and Double variables automatically, so convert one explicitly:
let a = 2
let b = 3
print(Double(7) / 3)
print(Double(b) / Double(a))
print(7.0 / 2)
2.3333333333333335
1.5
3.5
Order of evaluation
Multiplication, division and remainder bind tighter than addition and subtraction, as in school mathematics. Parentheses override that: 2 + 3 * 4 is 14, and (2 + 3) * 4 is 20.
Updating a variable
To store a result back in a var, combine the operator with =. Each of +=, -=, *=, /= and %= reads the variable, calculates and stores the result:
var total = 4 * 83
total += 15
total *= 2
total -= 1
total /= 3
total %= 100
print(total)
31
Step by step, total goes 332, 347, 694, 693, 231 and finally 31. The remainder is useful for questions like how many items are left over: with 7 items and boxes of 3, 7 / 3 is 2 full boxes and 7 % 3 is 1 item left.
The exercise
The constant price holds the cost of one product, 83. Andy buys 4 of them and one bag for 15, and the program must print the total.
- The products cost
4 * price. - The bag adds 15:
4 * price + 15. - Multiplication is evaluated before addition, so no parentheses are needed.
- Put the whole expression inside
print(...).
The solution in Swift
let price = 83
print(4 * price + 15)
It prints 347: 4 * 83 is 332, and 332 + 15 is 347.
Swift details
- Overflow is a run-time crash. An
Intholds values up to 9223372036854775807. Adding 1 to that limit in an ordinary expression stops the program with an error, rather than wrapping around. The wrapping operators&+,&-and&*wrap on purpose:Int.max &+ 1is-9223372036854775808. - Dividing an integer by zero also crashes.
10 / zerowithlet zero = 0stops the program. With aDoublezero, the result isinfinstead. Doublearithmetic is approximate.0.1 + 0.2prints0.30000000000000004.- Both sides need the same type.
Int * Doubleis a compile error, so convert withDouble(...).
Common mistakes
- Expecting
7 / 2to be 3.5. With integers it is 3. Use7.0 / 2. - Adding parentheses that change the meaning.
4 * (price + 15)is 392, not 347. - Forgetting
print. The expression is calculated, but nothing is shown. - Hard-coding 83. Use
price, so the result follows the constant.
Key takeaways
+ - * / %work on numbers, and*,/and%go before+and-.- Integer division truncates toward zero; use a
Doubleoperand for a fractional result. - A calculation produces a value and leaves its variables alone.
