curso-cpp-incremento-y-decremento

Increment and Decrement Operators in C++

  • 5 min

The increment (++) and decrement (--) operators are applied to variables to increase or decrease their value by one unit.

OperatorDescriptionBehavior
x++Post-incrementReturns the original value, then increments.
++xPre-incrementIncrements first, returns the updated value.
x--Post-decrementReturns the original value, then decrements.
--xPre-decrementDecrements first, returns the updated value.

The difference between increment and decrement is clear.

  • ++ adds one unit.
  • -- subtracts one unit.

The confusion arises between pre and post (i.e., whether it goes before or after the variable). The difference is that:

  • pre increments/decrements, and then returns the modified value.
  • post returns the original value, and then increments/decrements it.

When to use pre and when to use post?

I’ll make it easy for you. It doesn’t matter because you should always use them on their own line (I’ll give examples below).

Usage of Increment and Decrement Operators

Let’s look at the behavior of each one in more detail 👇

In the case of pre-increment (++variable), the ++ operator appears before the variable. This means the variable’s value is incremented before the variable is used in the expression where it appears.

#include <iostream>

int main() {
    int x = 5;
    int y = ++x;  // x is incremented first, then assigned to y

    std::cout << "Value of x: " << x << std::endl; // Result: 6
    std::cout << "Value of y: " << y << std::endl; // Result: 6
    return 0;
}
Copied!

In this case:

  • x is incremented to 6 before being assigned to y.
  • Both x and y will have the value 6.

In the case of post-increment (variable++), the ++ operator appears after the variable. This means the variable is used in the expression with its original value and then incremented.

#include <iostream>

int main() {
    int x = 5;
    int y = x++;  // x is assigned to y, then incremented

    std::cout << "Value of x: " << x << std::endl; // Result: 6
    std::cout << "Value of y: " << y << std::endl; // Result: 5
    return 0;
}
Copied!

In this case:

  • y takes the original value of x, which is 5.
  • Then x is incremented to 6.
  • The final result is x = 6 and y = 5.

With the pre-decrement operator (--variable), the variable’s value is decremented before it is used in the expression.

#include <iostream>

int main() {
    int x = 5;
    int y = --x;  // x is decremented first, then assigned to y

    std::cout << "Value of x: " << x << std::endl; // Result: 4
    std::cout << "Value of y: " << y << std::endl; // Result: 4
    return 0;
}
Copied!

Here:

  • x is decremented to 4 before being assigned to y.
  • Both values (x and y) are 4.

The post-decrement operator (variable--) first uses the variable’s original value and then decrements it.

#include <iostream>

int main() {
    int x = 5;
    int y = x--;  // x is assigned to y, then decremented

    std::cout << "Value of x: " << x << std::endl; // Result: 4
    std::cout << "Value of y: " << y << std::endl; // Result: 5
    return 0;
}
Copied!

In this example:

  • y takes the original value of x, which is 5.
  • Then x is decremented to 4.
  • The final result is x = 4 and y = 5.

Where to use them

As I said above, when you use pre and post increment/decrement operators, they should be on their own line.

For example, like this:

int x = 5;
int y = 10;
x++;  // on its own line

int result = x + y;
// result is 15
Copied!

The only notable exception I can think of is when they are used in for loops.

In the following example, we use i++ in the for loop to increment i at the end of each iteration.

#include <iostream>

int main() {
    for (int i = 0; i < 5; i++) {
        std::cout << "Iteration " << i << std::endl;
    }
    return 0;
}
Copied!

This is also fine (in reality, the ++ is still alone in its own single instruction).

Where NOT to use them

It’s a bad idea to use pre and post increment/decrement operators in complex expressions, because they lead to errors that are very difficult to detect.

That is, don’t do things like this.

int x = 5;
int y = 10;
int result = x++ + y;   // Post-increment: x is used as 5
// result is 15
Copied!
int x = 5;
int y = 10;
int result = ++x + y;   // Pre-increment: x is used as 6
// result is 16
Copied!