The increment (++) and decrement (--) operators are applied to variables to increase or decrease their value by one unit.
| Operator | Description | Behavior |
|---|---|---|
x++ | Post-increment | Returns the original value, then increments. |
++x | Pre-increment | Increments first, returns the updated value. |
x-- | Post-decrement | Returns the original value, then decrements. |
--x | Pre-decrement | Decrements 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;
}
In this case:
xis incremented to6before being assigned toy.- Both
xandywill have the value6.
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;
}
In this case:
ytakes the original value ofx, which is5.- Then
xis incremented to6. - The final result is
x = 6andy = 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;
}
Here:
xis decremented to4before being assigned toy.- Both values (
xandy) are4.
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;
}
In this example:
ytakes the original value ofx, which is5.- Then
xis decremented to4. - The final result is
x = 4andy = 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
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;
}
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
int x = 5;
int y = 10;
int result = ++x + y; // Pre-increment: x is used as 6
// result is 16
