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:
| Shorthand | Same as | Starting at | Result |
|---|---|---|---|
x += 5 | x = x + 5 | 10 | 15 |
x -= 5 | x = x - 5 | 10 | 5 |
x *= 5 | x = x * 5 | 3 | 15 |
x /= 4 | x = x / 4 | 20 | 5 |
x %= 3 | x = x % 3 | 11 | 2 |
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:
| Step | Statement | Value after |
|---|---|---|
| 1 | int number = 18; | 18 |
| 2 | number += 8; | 26 |
| 3 | number--; | 25 |
| 4 | number /= 2; | 12 |
| 5 | number++; | 13 |
| 6 | number *= 2; | 26 |
| 7 | number -= 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 =+ 8compiles, but it meansnumber = +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 numberappears 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= ymeansx = x op y, withxevaluated once.++and--add or subtract one.- On an
int,/=drops the fractional part.