Start learning free

LESSON 27 OF 414 · CALCULATIONS WITH NUMBERS

Increment and Decrement in Scala: += 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 (++).

Scala has no ++ operator. Instead, we increment a var with the augmented assignment += 1:

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

println(number) // Output: 11

Scala has no -- operator either. We decrement a variable, that is decrease its value by 1, with -= 1:

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

println(number) // Output: 9

Postfix:

In languages that have ++, it comes in two forms. In the postfix form, the operator comes after the operand (number++) and gives the value before incrementing.

In Scala, number += 1 doesn't give a value, but we can get the same effect by saving the old value before we increment:

var number: Int = 10

val oldNumber = number
number += 1

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

This code first assigns the value of number (which is 10) to oldNumber and then increments number (making it 11).


Prefix:

The other form is the prefix form, with the operator before the operand (++number), which gives the value after incrementing.

In Scala, we get the same effect by incrementing first and reading the value afterwards:

var number: Int = 10

number += 1
val newNumber = number

println(newNumber) // Output: 11
println(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 an example with decrementing:

var number: Int = 10

number -= 1
val newNumber = number

println(newNumber) // Output: 9
println(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

    SCALA · CALCULATIONS WITH NUMBERS

    Increment And Decrement Operators in Scala, explained

    Adding or subtracting one

    Raising a variable by 1, or lowering it by 1, is so common that many languages give it a dedicated operator, usually written ++ and --. Scala has neither. The idiomatic replacements are the augmented assignments += 1 and -= 1:

    var number: Int = 10
    number += 1
    println(number)
    number -= 1
    println(number)
    

    This prints 11, then 10. The statement number += 1 is short for number = number + 1.

    Both lines need number to be a var. A val can't be reassigned, so += on it doesn't compile.

    Why there is no ++

    If you write number++, the compiler reports value ++ is not a member of Int. A prefix form such as ++number fails too, with Not found: ++. Neither is a Scala operator, so the compound assignment is the only short form to remember.

    Post-increment and pre-increment, done by hand

    In languages that have ++, the two forms differ in what the expression gives back. The postfix form returns the value from before the change, and the prefix form returns the value from after it. In Scala, number += 1 is a statement that changes the variable but gives back no useful value: its result is (), the single value of type Unit. So you can't capture the result of the increment itself, and you use two lines instead. The order of the lines gives you the same effect.

    Value before the change

    Read the variable first, then change it:

    var number: Int = 10
    val oldNumber = number
    number += 1
    println(oldNumber)
    println(number)
    

    This prints 10, then 11. oldNumber is a copy of the number taken before the change, so it keeps the value 10.

    Value after the change

    Change the variable first, then read it:

    var number: Int = 10
    number += 1
    val newNumber = number
    println(newNumber)
    println(number)
    

    This prints 11, then 11. The same idea works for decrementing: with number -= 1 in place of number += 1, both lines print 9.

    The copy never changes afterwards, because an Int is a plain value, not a reference to the variable. Later changes to number don't affect oldNumber or newNumber. They are vals as well, so they can't be reassigned.

    The lesson's exercise

    The exercise walks through all of these ideas on one variable. Here is the value after each step:

    1. Create number with var and the value 18.
    2. number += 1 makes it 19.
    3. Copy it into post first, then increment. post holds 19 and number becomes 20.
    4. Increment again, then copy into pre. number becomes 21 and pre holds 21.
    5. number -= 1 brings it back to 20.

    The solution:

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

    Notice that the third step takes post before its increment, while pre is taken after the increment. That difference in order is the entire post-versus-pre distinction. At the end, post is 19, pre is 21 and number is 20.

    Common mistakes

    • Writing number++ or ++number. Neither compiles in Scala.
    • Using val for the counter. The first += fails. Use var for values that change.
    • Trying to capture the increment's result. val k = (number += 1) stores (), not the new number. Read number on its own line.
    • Swapping the order. Copying after the increment gives the new value even when you wanted the old one.

    Key takeaways

    • Scala has no ++ or --; use += 1 and -= 1 on a var.
    • The compound assignment changes the variable but returns Unit.
    • To get the old value, copy the variable before changing it. To get the new value, copy it after.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free