Increment and compound assignment
The operators of the previous lesson computed a new value, and the variables stayed as they were. Often you need something else: to change the variable itself—add points, take money off, count visitors.
To add 5 to a variable, you can write score = score + 5. C# first computes the right side with the old value of score, then puts the result back into the same variable. You have to write the name twice, and a long name invites a typo.
A calculator has the "M+" button for this job. It adds the number on the screen to what is already in memory, and you do not type the old value again. In C# that button is the += operator:
int score = 10;
score += 5;
Console.WriteLine(score); // 15
score -= 3;
Console.WriteLine(score); // 12
score *= 2;
Console.WriteLine(score); // 24
score /= 5;
Console.WriteLine(score); // 4
score %= 3;
Console.WriteLine(score); // 1Let's follow how score changed:
score += 5adds5: it was10, it becomes15.score -= 3subtracts3, and you get12.score *= 2multiplies by2, and you get24.score /= 5divides by5. The division is integer, so24 / 5gives4, not4.8.score %= 3puts the remainder of dividing by3intoscore, which is1.
Compound assignment
score += 5 means the same as score = score + 5. This form is called compound assignment: it joins the arithmetic and the assignment into one operator. Each arithmetic operation has a compound form of its own:
+=adds;-=subtracts;*=multiplies;/=divides, and forintit also drops the fractional part;%=keeps the remainder.
This is where the calculator analogy ends: a calculator has only "M+" and "M−", while C# can write all five operations this way.
The += operator works with strings too. After string name = "C"; and name += "#";, name holds "C#": + with a string joins text.
Increment and decrement
Adding and subtracting one is so common that it has operators of its own. The increment visitors++ makes the variable larger by 1, and the decrement visitors-- makes it smaller. They are the same as visitors += 1 and visitors -= 1.
A visitor counter at an entrance works this way: each press of the button adds one person.
int visitors = 0;
visitors++;
visitors++;
visitors--;
Console.WriteLine(visitors); // 1Two increments give 2, and the decrement brings it back to 1.
Prefix and postfix
++ and -- have two forms: after the name (a++) and before it (++a). Both forms act on the variable itself the same way: ++ makes it larger by 1, and -- makes it smaller. The difference shows when the result of the operation is used right away, for example assigned to another variable:
int a = 5;
int b = a++;
Console.WriteLine(a); // 6
Console.WriteLine(b); // 5
int c = ++a;
Console.WriteLine(a); // 7
Console.WriteLine(c); // 7Postfix a++
The postfix form remembers the old value, increases the variable and gives back the remembered one. In int b = a++;, a becomes 6, and b gets 5.
Prefix ++a
The prefix form first increases the variable, then gives the new value. In int c = ++a;, a becomes 7, and c gets 7 too.
When a++; or ++a; stands on its own line, there is no difference: nobody reads the result, and both forms only increase a.
Interviewers like to ask how i++ differs from ++i. The answer: both forms increase the variable by one, and they differ in the value they give. The postfix form gives the old value, the prefix form gives the new one.
Common mistake
A variable is incremented, and the result is assigned straight back to it:
int x = 5;
x = x++; // ✗
Console.WriteLine(x); // 5The compiler says nothing, and x stays 5. Let's go step by step:
x++keeps the old value5—that is its result.xbecomes6.- The assignment puts the result of
x++, which is5, intox, and the increase is lost.
The rules of C# fix this order, so the program always prints 5. To increase x, write x++; on its own line, with no assignment.
Key points
score += 5is the same asscore = score + 5;-=,*=,/=and%=work the same way.x++makes the variable larger by one,x--makes it smaller.- Postfix
x++gives the old value, prefix++xgives the new one. x = x++does not changex: writex++;on its own line.