Start learning free

LESSON 26 OF 347 · CALCULATIONS WITH NUMBERS

Increment and Decrement Operators in C#: ++ and --

Lesson Read the full explanation

In programming, increasing the value of a variable by 1 is so common, there is an operator designed to do that.

That operator is the increment operator (++):

int number = 10;
number++; // short for number += 1

Console.WriteLine(number); // Output: 11

There is also one such operator for decreasing the value of a variable by 1.

That operator is the decrement operator (--):

int number = 10;
number--; // short for number -= 1

Console.WriteLine(number); // Output: 9

Postfix:

In the examples above, we have used these two operators in their postfix form, with operator (++) after operand (number) such as number++.

If used postfix, the increment operator increments and returns the value before incrementing:

int number = 10;

int oldNumber = number++;

Console.WriteLine(oldNumber); // Output: 10
Console.WriteLine(number); // Output: 11

This code first assigns the value of number (which is 10) to oldNumber and then increments number (making it 11). It's equivalent to this:

int oldNumber = number;
number++;

Prefix:

However, there is also the prefix form, with operator before operand (for example, ++number).

When used prefix, the increment operator increments and returns the value after incrementing:

int number = 10;

int newNumber = ++number;

Console.WriteLine(newNumber); // Output: 11
Console.WriteLine(number); // Output: 11

This code first increments number (making it 11) and then assigns the new value of number to oldNumber (making it 11). It's equivalent to this:

number++;
int newNumber = number;

Here's an example with the decrement operator:

int number = 10;

int newNumber = --number;

Console.WriteLine(newNumber); // Output: 9
Console.WriteLine(number); // Output: 9

Assignment
Follow the Coding Tutorial and let's practice with these operators!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Increment And Decrement Operators in C#, explained

    What the operators do

    Adding 1 to a variable or taking 1 away is so common that C# has a short form for each. The increment operator ++ adds 1, and the decrement operator -- subtracts 1:

    int number = 10;
    number++;   // same as number += 1
    Console.WriteLine(number);   // 11
    
    number--;   // same as number -= 1
    Console.WriteLine(number);   // 10
    

    Both operators change the variable itself, so the variable must be a real, assignable variable. That means it can't be a const, and it can't be a literal: 5++ is a compile error. They work on the other numeric types too. A double goes up by 1.0, and a char moves to the next character, so 'a' becomes 'b'.

    Postfix and prefix forms

    Each operator can go after the variable (number++, postfix) or before it (++number, prefix). On a line of their own, the two forms do exactly the same thing. The difference appears only when you use the expression's result, for example by assigning it to another variable.

    • Postfix (number++) gives you the value from before the change, then changes the variable.
    • Prefix (++number) changes the variable first, then gives you the value after the change.
    int number = 10;
    int oldNumber = number++;
    Console.WriteLine(oldNumber);   // 10
    Console.WriteLine(number);      // 11
    
    int other = 10;
    int newNumber = ++other;
    Console.WriteLine(newNumber);   // 11
    Console.WriteLine(other);       // 11
    

    The decrement operator follows the same rule. int old = n--; with n equal to 10 leaves old as 10 and n as 9, while int q = --n; would give q the new value 9.

    A handy way to remember it: the operator's position tells you when the change is visible to the rest of the expression. If the operator comes first, you see the change straight away. If the name comes first, you see the old value.

    Most of the time you'll use them as counters: a variable that goes up by one each time something happens, such as a score or the number of items seen so far, or down by one as something is used up. In that role the prefix and postfix forms are interchangeable, and many programmers default to the postfix one.

    Working through the exercise

    The exercise builds one variable up step by step and records what each form returns. Follow the value of number as you go:

    1. Create int number = 18;. It has to be an ordinary variable, because the operators modify it.
    2. Write number++; on its own line. Nothing uses the result, so number becomes 19.
    3. Write int post = number++;. The postfix form hands back the old value, so post is 19, and then number becomes 20.
    4. Write int pre = ++number;. The prefix form increments first, so number becomes 21 and pre is 21.
    5. Write --number; to take 1 away. Now number is 20, while post stays 19 and pre stays 21.

    The finished code is short:

    int number = 18;
    number++;
    int post = number++;
    int pre = ++number;
    --number;
    

    Notice that post and pre are ordinary copies. They hold plain numbers, and later changes to number don't affect them.

    Gotchas

    Assigning a variable its own postfix result

    This line looks like it should add 1, but it doesn't:

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

    x++ produces the old value 5 and bumps x to 6, and then the assignment writes the old value 5 back into x. If you just want to add 1, write x++; by itself.

    Using the variable twice in one expression

    C# evaluates an expression from left to right, so the change is already visible to whatever comes after it:

    int a = 5;
    int b = a++ + a;
    Console.WriteLine(b);   // 5 + 6 = 11
    

    Code like this is correct but hard to read. Prefer a separate a++; statement when you can.

    Array indexes

    The postfix form is common when stepping through an array, because it uses the current index and then moves on:

    int[] values = { 10, 20, 30 };
    int i = 0;
    Console.WriteLine(values[i++]);   // 10
    Console.WriteLine(values[i++]);   // 20
    Console.WriteLine(i);             // 2
    

    Overflow

    An int has a largest value, int.MaxValue. By default C# doesn't complain when you pass it: int m = int.MaxValue; m++; silently wraps around to -2147483648.

    Key takeaways

    • ++ adds 1 and -- subtracts 1, changing the variable in place.
    • On a line of their own, x++ and ++x are identical.
    • Postfix returns the old value; prefix returns the new value.
    • Don't write x = x++; it leaves x unchanged.
    • Keep the operators in simple statements, and the order of evaluation never becomes a puzzle.
    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