Start learning free

LESSON 26 OF 414 · CALCULATIONS WITH NUMBERS

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

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
println(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
println(myVar) // Output: 5

For multiplication: *=

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

For division: /=

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

For remainder: %=

var myVar = 11
myVar %= 3 // short for myVar = myVar % 3
println(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

    SCALA · CALCULATIONS WITH NUMBERS

    Augmented Assignment Operators in Scala, explained

    What augmented assignment is

    Changing a variable based on its own value is one of the most common things a program does. In Scala you can write it out in full:

    var myVar = 10
    myVar = myVar + 5
    println(myVar)
    

    The right side of = is computed first, using the old value, and the result is stored back into the variable. This prints 15. Because the pattern is so common, Scala offers a shorter form, the augmented assignment:

    var myVar = 10
    myVar += 5
    println(myVar)
    

    It does the same thing and also prints 15. There is one operator for each arithmetic operator:

    OperatorShort forExampleResult
    +=x = x + yx = 10; x += 515
    -=x = x - yx = 10; x -= 55
    *=x = x * yx = 3; x *= 515
    /=x = x / yx = 20; x /= 45
    %=x = x % yx = 11; x %= 32

    Rules that matter in Scala

    The variable must be a var

    An augmented assignment changes the variable, so the variable has to be declared with var. A val can't be reassigned. If you write n += 1 where n is a val, the compiler reports:

    value += is not a member of Int
    

    The message looks odd, but the fix is simple: declare the variable with var. Scala infers the type Int from a literal such as 18, so you don't need to write it.

    Integer division drops the remainder

    When both sides are Int, /= performs integer division and throws away the fractional part:

    var d = 26
    d /= 5
    println(d)
    

    This prints 5, not 5.2. Division truncates toward zero, so a variable holding -7 becomes -3 after /= 2. Use %= when you want the remainder instead.

    The result stays an Int

    The variable keeps its type. With an Int variable, n += 1.5 doesn't compile (the compiler reports the same value += is not a member of Int message), because the sum would be a Double and can't be stored back in an Int. Make the variable a Double or a Long if you need that range of values.

    An Int also has a fixed size of 32 bits, and overflow wraps around silently:

    var big = Int.MaxValue
    big += 1
    println(big)
    

    This prints -2147483648. A Long variable has 64 bits and holds much larger values.

    There is no ++ or --

    Scala has no increment or decrement operators. Writing n++ is a compile error: value ++ is not a member of Int. Use n += 1 and n -= 1 instead.

    It works on strings too

    += isn't limited to numbers. On a var holding a String, it appends text:

    var s = "ab"
    s += "cd"
    println(s)
    

    This prints abcd.

    An assignment has no useful value

    The expression n += 1 changes n but gives back Unit, Scala's "no useful value". Writing val r = { n += 1 } stores () in r, not the new number. To use the new value, read the variable on the next line.

    The lesson's exercise

    The exercise is a drill that applies one operator after another to a single variable. Follow the value through each step:

    1. Create number with var and the value 18.
    2. number += 8 makes it 26.
    3. number -= 1 makes it 25.
    4. number /= 5 makes it 5. The division is exact here, so nothing is lost.
    5. number += 1 makes it 6.
    6. number *= 3 makes it 18.
    7. number -= 18 makes it 0.

    The complete solution:

    var number = 18
    number += 8
    number -= 1
    number /= 5
    number += 1
    number *= 3
    number -= 18
    

    Write down the value after every line, as in the list above. Most mistakes in this drill come from losing track of the current value, especially after the division.

    Common mistakes

    • Using val. The first operator that tries to change the variable fails to compile.
    • Reaching for ++. It doesn't exist in Scala. Use += 1.
    • Expecting a decimal from /=. Two integers divide to an integer.
    • Mixing up =+ and +=. the operator always starts with the symbol, so x =+ 5 is read as one unknown operator =+ and doesn't compile.

    Key takeaways

    • x op= y is short for x = x op y, with operators +=, -=, *=, /= and %=.
    • The variable must be a var, and it keeps its type.
    • Integer division truncates, and Int overflow wraps silently.
    • Scala has no ++ or --; use += 1 and -= 1.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free