Start learning free

LESSON 27 OF 415 · CALCULATIONS WITH NUMBERS

Increment and Decrement in Swift: += 1 and -= 1

Lesson Read the full explanation

In programming, increasing the value of a variable by 1 is so common that many languages have an operator designed to do that: the increment operator (++).

Swift has no ++ operator (it was removed in Swift 3). Instead, you use the augmented assignment += with 1:

var number = 10
number += 1 // short for number = number + 1

print(number) // Output: 11

Other languages also have an operator for decreasing the value of a variable by 1, the decrement operator (--). Swift has no -- either, so you use -= with 1:

var number = 10
number -= 1 // short for number = number - 1

print(number) // Output: 9

Postfix:

In languages that have them, the operators can be used in their postfix form, with the operator after the operand, such as number++.

If used postfix, the increment operator increments and returns the value before incrementing. Swift has no such operator, but you can get the same result by reading the value first and then incrementing:

var number = 10

var oldNumber = number
number += 1

print(oldNumber) // Output: 10
print(number) // Output: 11

This code first assigns the value of number (which is 10) to oldNumber and then increments number (making it 11). That is what a postfix increment does in other languages.


Prefix:

There is also the prefix form, with the operator before the operand (for example, ++number in other languages).

When used prefix, the increment operator increments and returns the value after incrementing. In Swift you write it as two statements:

var number = 10

number += 1
var newNumber = number

print(newNumber) // Output: 11
print(number) // Output: 11

This code first increments number (making it 11) and then assigns the new value of number to newNumber (making it 11).

Here's the same idea with a decrement:

var number = 10

number -= 1
var newNumber = number

print(newNumber) // Output: 9
print(number) // Output: 9

Assignment
Follow the Coding Tutorial and let's practice with these operators!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    SWIFT · CALCULATIONS WITH NUMBERS

    Increment And Decrement Operators in Swift, explained

    Adding and subtracting one

    Adding 1 to a variable is so common that many languages give it a dedicated operator, ++, and subtracting 1 gets --. Swift does not have either of them. Swift 3 removed them, and the idiomatic way to write the same thing is a compound assignment:

    var number = 10
    number += 1
    print(number) // 11
    
    number -= 1
    print(number) // 10
    

    number += 1 means "take the current value of number, add 1 and store the result back in number". -= does the same with subtraction. The variable has to be declared with var. A let constant can't change, so the compiler rejects the operator:

    error: left side of mutating operator isn't mutable: 'number' is a 'let' constant
    

    Writing number++ is rejected as well, with an error that points you to the replacement:

    error: cannot find operator '++' in scope; did you mean '+= 1'?
    

    Post-increment and pre-increment

    In languages that have ++, the operator comes in two forms. The postfix form (x++) gives back the value from before the change. The prefix form (++x) gives back the value after it. This lesson reproduces both behaviours in Swift, and the trick is the order of two statements: read the value, and change it.

    Post-increment: read first, then change

    var count = 5
    let before = count // read the old value
    count += 1         // then change the variable
    
    print(before) // 5
    print(count)  // 6
    

    before is a separate constant. It received a copy of the number, so the later change to count doesn't touch it.

    Pre-increment: change first, then read

    var count = 5
    count += 1
    let after = count // read the new value
    
    print(after) // 6
    print(count) // 6
    

    Both constants end up as snapshots of count taken at different moments. The same pattern works for decrement, with -= 1 in place of += 1.

    Unlike ++ in other languages, += in Swift is a statement that changes the variable and produces no value you can use. You can't write let x = (count += 1) and expect the old or new number. That is why the pattern needs a separate line to capture the value.

    The exercise, step by step

    1. Create a variable. var number = 18. It must be var, because every following step changes it.
    2. Increment it. number += 1 takes it from 18 to 19.
    3. Post-increment. Save the current value in a constant named post, then increment. post keeps 19 while number becomes 20.
    4. Pre-increment. Increment first, then save the value in a constant named pre. After step 3 incremented once, this step increments again, so number is 21 and pre holds the new value, 21.
    5. Decrement. number -= 1 brings number down by one.

    The solution in Swift

    var number = 18
    number += 1
    let post = number
    number += 1
    number += 1
    let pre = number
    number -= 1
    

    Tracing the values line by line:

    LinenumberOther values
    var number = 1818
    number += 119
    let post = number19post is 19
    number += 120
    number += 121
    let pre = number21pre is 21
    number -= 120

    At the end post is 19, pre is 21 and number is 20.

    Common mistakes

    • Using let for the counter. A constant can't be incremented. Use var for anything that changes.
    • Writing number++ or --number. They don't exist in Swift. Use += 1 and -= 1.
    • Capturing the value in the wrong place. If you move let post = number below the increment, it holds the new value, which makes it a pre-increment. The position of the capture line is the whole difference.
    • Using post or pre as variables. They are constants, so a later post += 1 is an error. They record a moment, and the changing value stays in number.
    • Overflow. Int is 64 bits in Swift. Adding 1 to Int.max crashes the program at run time instead of wrapping around.

    Key takeaways

    • Swift has no ++ or --. Use += 1 and -= 1 on a var.
    • "Post" means read, then change. "Pre" means change, then read.
    • A let constant records a value at one moment and never follows later changes.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free