Skip to content
Theory · Operators · lesson 3 of 10

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:

C#
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); // 1

Let's follow how score changed:

  1. score += 5 adds 5: it was 10, it becomes 15.
  2. score -= 3 subtracts 3, and you get 12.
  3. score *= 2 multiplies by 2, and you get 24.
  4. score /= 5 divides by 5. The division is integer, so 24 / 5 gives 4, not 4.8.
  5. score %= 3 puts the remainder of dividing by 3 into score, which is 1.

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 for int it 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.

C#
int visitors = 0;
visitors++;
visitors++;
visitors--;
Console.WriteLine(visitors); // 1

Two 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:

C#
int a = 5;
int b = a++;
Console.WriteLine(a); // 6
Console.WriteLine(b); // 5
int c = ++a;
Console.WriteLine(a); // 7
Console.WriteLine(c); // 7

Postfix 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:

C#
int x = 5;
x = x++; // ✗
Console.WriteLine(x); // 5

The compiler says nothing, and x stays 5. Let's go step by step:

  1. x++ keeps the old value 5—that is its result.
  2. x becomes 6.
  3. The assignment puts the result of x++, which is 5, into x, 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 += 5 is the same as score = 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 ++x gives the new one.
  • x = x++ does not change x: write x++; on its own line.