Increment (++
) and decrement (--
) operators are applied to variables to increase or decrease their value by one unit.
Operator | Description | Behavior |
---|---|---|
x++ | Pre-increment | Increments first, returns the updated value. |
++x | Post-increment | Returns the original value, then increments. |
x-- | Pre-decrement | Decrements first, returns the updated value. |
--x | Post-decrement | Returns the original value, then decrements. |
The difference between increment and decrement is clear.
++
adds one unit--
subtracts one unit.
The doubt arises between pre and post (that is, 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
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 in their own line (below I give you examples)
Using Increment and Decrement Operators
Let’s look at the behavior of each of them in more detail 👇
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 to6
before being assigned toy
.- Both
x
andy
will have the value6
.
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 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 ofx
, which is5
.- Then
x
is incremented to6
. - The final result is
x = 6
andy = 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 to4
before being assigned toy
.- Both values (
x
andy
) are4
.
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 ofx
, which is5
.- Then
x
is decremented to4
. - The final result is
x = 4
andy = 5
.
Where to USE them
As I mentioned above, when using pre and post increment/decrement operators, it should be in their own line.
For example, like this:
int x = 5;
int y = 10;
x++; // in 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 (actually, the ++ is still on its own in a single instruction)
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