Start learning free

LESSON 25 OF 347 · CALCULATIONS WITH NUMBERS

Augmented Assignment Operators in C#: +=, -=, *=, /=, ++ 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:

int myVar = 10;
myVar += 5;
Console.WriteLine(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: -=

int myVar = 10;
myVar -= 5; // short for myVar = myVar - 5;
Console.WriteLine(myVar); // Output: 5

For multiplication: *=

int myVar = 3;
myVar *= 5; // short for myVar = myVar * 5;
Console.WriteLine(myVar); // Output: 15

For division: /=

int myVar = 20;
myVar /= 4; // short for myVar = myVar / 4;
Console.WriteLine(myVar); // Output: 5

For remainder: %=

int myVar = 11;
myVar %= 3; // short for myVar = myVar % 3;
Console.WriteLine(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

    C# · CALCULATIONS WITH NUMBERS

    Augmented Assignment Operators in C#, explained

    Changing a variable using its own value

    Programs constantly update a variable based on what it already holds. You could write that out in full:

    int myVar = 10;
    myVar = myVar + 5;
    

    The right side is evaluated first, using the old value (10 + 5), and the result is stored back in the same variable. Because this pattern is so common, C# has shorthand operators that do the arithmetic and the assignment in one step. They are called augmented assignment or compound assignment operators.

    int myVar = 10;
    myVar += 5;
    Console.WriteLine(myVar);  // 15
    

    The operators

    There is one for each arithmetic operator:

    ShorthandSame asStarting atResult
    x += 5x = x + 51015
    x -= 5x = x - 5105
    x *= 5x = x * 5315
    x /= 4x = x / 4205
    x %= 3x = x % 3112

    The variable must already exist and hold a value, because the operator reads it before writing to it. Writing x += 5 on a variable that was never assigned is a compile error.

    Increment and decrement

    Adding or subtracting exactly 1 is so frequent that it has its own operators. x++ means the same as x += 1, and x-- means the same as x -= 1. Used as a statement on its own line, as in this lesson, they do exactly that and nothing more.

    These operators can also appear inside a larger expression, and then the position matters: y = x++ stores the old value of x in y and then increments, while y = ++x increments first. With x equal to 5, the first form leaves y at 5 and x at 6, and the second then moves both to 7. Beginners should keep ++ and -- on their own lines until this feels natural.

    Integer division inside /=

    With an int variable, /= uses integer division, so the fraction is lost. int n = 10; n /= 4; leaves n equal to 2, not 2.5. With a double variable, the same statement leaves 2.5. This is the same rule as for plain /, now applied to the shorthand.

    Mixing int and double

    The shorthand operators follow the usual type rules. A double variable can take any of them with whole or fractional values: double d = 7; d += 2.5; makes d equal to 9.5. An int variable, however, cannot take a double operand. int s = 7; s += 2.5; does not compile, because the result would have to be squeezed back into an int. Either declare the variable as double or cast the operand deliberately with (int)2.5, which truncates to 2. In this lesson everything is an int, so the issue does not come up.

    The exercise, step by step

    The lesson is a drill. You write seven statements, one per step, and track the value of number as it changes:

    StepStatementValue after
    1int number = 18;18
    2number += 8;26
    3number--;25
    4number /= 2;12
    5number++;13
    6number *= 2;26
    7number -= 26;0

    Step 4 is the one to watch: 25 divided by 2 is 12.5, but number is an int, so it keeps 12. Everything after that depends on 12, which is why the later values are 13 and 26. Step 7 subtracts 26 because step 6 produced 26, so the variable ends at 0.

    The solution in C#

    int number = 18;
    number += 8;
    number--;
    number /= 2;
    number++;
    number *= 2;
    number -= 26;
    

    This code prints nothing. It only changes number, and the tests check the final value. To watch the changes while you experiment, add a Console.WriteLine(number); after any step and remove it again when you finish.

    Common mistakes

    • Writing =+ instead of +=. number =+ 8 compiles, but it means number = +8, which assigns 8 and discards the old value. The operator character comes first.
    • Spaces inside the operator. + = is a syntax error. The two characters must be adjacent.
    • Declaring the variable again. int number appears only in step 1. Later steps just use the name.
    • Forgetting the integer division. If you expect 12.5 after step 4, every later value is off.
    • Using the shorthand to declare. int x += 1; is invalid, because the variable must exist first.

    Key takeaways

    • x op= y means x = x op y, with x evaluated once.
    • ++ and -- add or subtract one.
    • On an int, /= drops the fractional part.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free