Updating a variable
Programs often change a variable based on its current value. In Swift the right side of = is evaluated first, so this line adds 5 to myVar:
myVar = myVar + 5
The pattern is so common that Swift has a shorter form, the augmented assignment operators. They combine an arithmetic operator with the assignment.
var myVar = 10
myVar += 5
print(myVar) // 15
myVar += 5 means myVar = myVar + 5. The variable name is written once instead of twice.
One operator for each operation
| Operator | Same as | Example | Result |
|---|---|---|---|
+= | x = x + y | 10 then x += 5 | 15 |
-= | x = x - y | 10 then x -= 5 | 5 |
*= | x = x * y | 3 then x *= 5 | 15 |
/= | x = x / y | 20 then x /= 4 | 5 |
%= | x = x % y | 11 then x %= 3 | 2 |
Swift specifics
- The variable must be declared with
var. On aletconstant,n += 1fails withleft side of mutating operator isn't mutable: 'n' is a 'let' constant. - There is no
++or--. They were removed in Swift 3. Writingn++givescannot find operator '++' in scope; did you mean '+= 1'?. Usen += 1andn -= 1. /=follows the variable's type. For anInt, it divides and drops the fraction: starting from 26,x /= 5leaves 5. For aDouble, starting from 7.0,x /= 2leaves 3.5.- An augmented assignment is a statement. It changes the variable and returns nothing useful (its type is
Void), so it doesn't belong inside a larger expression. - The types still have to match. You can't write
+=with aDoubleon the right of anIntvariable without converting it.
Why use them
The short form is easier to read, because the intent is visible at a glance: this variable is being updated, not replaced by something unrelated. It also avoids typos in the repeated name, and the variable is only mentioned once.
Beyond arithmetic
The most common use is accumulating a total in a loop. Starting from var total = 0, adding 4, 6 and 10 with total += price leaves 20. The operator also works on strings: after var s = "Hello", the line s += ", Swift" makes s equal to "Hello, Swift".
The exercise: a drill of seven steps
The lesson builds one variable called number and changes it step by step. Each step uses a different operator. Tracking the value after each one helps check your work:
var number = 18creates the variable. It usesvarbecause every later line changes it. Swift infersIntfrom the literal 18.number += 8adds 8 and gives 26.number -= 1subtracts 1 and gives 25.number /= 5divides by 5 and gives 5. Both values are integers, and 25 divides evenly.number += 1adds 1 and gives 6.number *= 3multiplies by 3 and gives 18.number -= 18subtracts 18 and brings it to 0.
The solution in Swift
var number = 18
number += 8
number -= 1
number /= 5
number += 1
number *= 3
number -= 18
Adding print(number) after each line shows the values 26, 25, 5, 6, 18 and 0 in turn. The lesson's code itself prints nothing, because it never calls print.
Common mistakes
- Writing
=+instead of+=. The operator is the arithmetic sign followed by the equals sign.number =+ 8fails withcannot find operator '=+' in scope. - Using
let. A constant can't be updated. - Losing track of the value. Because each step uses the previous result, a mistake early on carries through. Check the running value after each line.
- Dividing when the result isn't whole. From 26,
number /= 5leaves 5, not 5.2. Here 25 divides evenly, so nothing is lost.
Key takeaways
x += yis shorthand forx = x + y, and the same goes for-=,*=,/=and%=.- The variable must be a
var. - Swift has no
++or--; use+= 1and-= 1.
