Start learning free

LESSON 26 OF 415 · CALCULATIONS WITH NUMBERS

Augmented Assignment Operators in Swift: +=, -=, *=, /= and %=

Lesson Read the full explanation

In programming, it is common to use assignments to modify the contents of a variable.

Remember that everything to the right of the equals sign (=) is evaluated first, so we can say:

myVar = myVar + 5

to add 5 to myVar.

Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.

One such operator is the += operator:

var myVar = 10
myVar += 5
print(myVar) // Output: 15

This code would print 15 to the console. We call myVar += 5 an augmented assignment.

There is one such operator for every other arithmetic operator we've learned:

For subtraction: -=

var myVar = 10
myVar -= 5 // short for myVar = myVar - 5
print(myVar) // Output: 5

For multiplication: *=

var myVar = 3
myVar *= 5 // short for myVar = myVar * 5
print(myVar) // Output: 15

For division: /=

var myVar = 20
myVar /= 4 // short for myVar = myVar / 4
print(myVar) // Output: 5

For remainder: %=

var myVar = 11
myVar %= 3 // short for myVar = myVar % 3
print(myVar) // Output: 2

Assignment
Follow the Coding Tutorial and let's do some augmented assignments.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 7
    Ctrl + Enter to check

    SWIFT · CALCULATIONS WITH NUMBERS

    Augmented Assignment Operators in Swift, explained

    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

    OperatorSame asExampleResult
    +=x = x + y10 then x += 515
    -=x = x - y10 then x -= 55
    *=x = x * y3 then x *= 515
    /=x = x / y20 then x /= 45
    %=x = x % y11 then x %= 32

    Swift specifics

    • The variable must be declared with var. On a let constant, n += 1 fails with left side of mutating operator isn't mutable: 'n' is a 'let' constant.
    • There is no ++ or --. They were removed in Swift 3. Writing n++ gives cannot find operator '++' in scope; did you mean '+= 1'?. Use n += 1 and n -= 1.
    • /= follows the variable's type. For an Int, it divides and drops the fraction: starting from 26, x /= 5 leaves 5. For a Double, starting from 7.0, x /= 2 leaves 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 a Double on the right of an Int variable 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:

    1. var number = 18 creates the variable. It uses var because every later line changes it. Swift infers Int from the literal 18.
    2. number += 8 adds 8 and gives 26.
    3. number -= 1 subtracts 1 and gives 25.
    4. number /= 5 divides by 5 and gives 5. Both values are integers, and 25 divides evenly.
    5. number += 1 adds 1 and gives 6.
    6. number *= 3 multiplies by 3 and gives 18.
    7. number -= 18 subtracts 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 =+ 8 fails with cannot 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 /= 5 leaves 5, not 5.2. Here 25 divides evenly, so nothing is lost.

    Key takeaways

    • x += y is shorthand for x = x + y, and the same goes for -=, *=, /= and %=.
    • The variable must be a var.
    • Swift has no ++ or --; use += 1 and -= 1.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free