Language: EN

curso-cpp-incremento-y-decremento

Increment and Decrement Operators in C++

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

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

The difference between increment and decrement is clear.

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

The question arises between pre and post (that is, whether it comes 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

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 you examples below)

Using the increment and decrement operators

Let’s take a closer look at the behavior of each of them 👇

In the case of pre-increment (++variable), the ++ operator appears before the variable. This means that 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;
}

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 that the variable is used in the expression with its original value and then is 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;
}

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 value of the variable 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;
}

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 original value of the variable 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;
}

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 mentioned above, when you use increment/decrement operators, they should be on their own line.

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

Where NOT to USE them

It is a bad idea to use pre and post increment/decrement operators in complex expressions, as they can lead to very hard-to-detect errors.

That is, do not do things like this

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