C++ Increment and Decrement Operators
The ++ and -- operators are everywhere in C++ — especially in loops. They look simple, but the difference between ++i and i++ trips up almost every beginner. Let’s make it crystal clear.
What ++ and — Actually Do
The increment operator ++ adds 1 to a variable. The decrement operator -- subtracts 1. They’re just shorthand:
#include <iostream>
int main() {
int score = 10;
score++; // same as score = score + 1
std::cout << score << "\n"; // 11
int lives = 3;
lives--; // same as lives = lives - 1
std::cout << lives << "\n"; // 2
return 0;
}
So far so good. The interesting part is that each operator has two forms: one where the symbol comes before the variable, and one where it comes after.
Pre-Increment vs Post-Increment: The Real Difference
Both ++i and i++ change i by the same amount. The difference is the value the expression produces while doing it:
++i(pre-increment): add 1 first, then hand back the new value.i++(post-increment): hand back the old value first, then add 1.
You only notice this when you use the result of the expression in the same line:
#include <iostream>
int main() {
int a = 5;
int b = a++; // b gets the OLD value (5), then a becomes 6
std::cout << "a = " << a << ", b = " << b << "\n"; // a = 6, b = 5
int c = 5;
int d = ++c; // c becomes 6 FIRST, then d gets 6
std::cout << "c = " << c << ", d = " << d << "\n"; // c = 6, d = 6
return 0;
}
Read a++ as “give me a, then increment.” Read ++a as “increment, then give me a.” That one sentence is the whole concept.
Which Should You Use in a for Loop?
Here’s the good news: in an ordinary counting loop, it makes no difference at all.
for (int i = 0; i < 5; i++) // works
for (int i = 0; i < 5; ++i) // identical result
The loop ignores the value the expression produces — it only cares about the side effect of adding 1. For a plain int, compilers generate the exact same machine code either way, so there’s no performance cost.
So why do experienced programmers prefer ++i? Because of iterators and objects, covered next.
Why Pros Prefer ++i
When you loop over a container with an iterator (or any custom object), post-increment has to make a copy of the old value to return it, then increment the original. Pre-increment skips the copy entirely:
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {10, 20, 30};
for (auto it = nums.begin(); it != nums.end(); ++it) {
std::cout << *it << "\n";
}
return 0;
}
For a heavyweight object, that saved copy can actually matter. Since ++it is never slower and sometimes faster, ++i is the safe default habit — even on plain integers where it doesn’t matter.
A Gotcha: Don’t Increment the Same Variable Twice
Avoid writing something clever like i = i++; or arr[i] = i++;. Modifying the same variable more than once in a single statement leads to confusing, unreliable results. Keep each statement doing one clear thing:
// Unclear and error-prone:
// i = i++;
// Clear:
i++;
If a line is hard to read, split it up. Readable code beats clever code every time.
The Decrement Operators Work the Same Way
Everything above applies to --, just in the opposite direction. --i subtracts 1 then returns the new value; i-- returns the old value then subtracts 1. Countdown loops use it constantly:
#include <iostream>
int main() {
for (int i = 5; i > 0; --i) {
std::cout << i << " ";
}
std::cout << "Liftoff!\n"; // 5 4 3 2 1 Liftoff!
return 0;
}
Quick Reference
| Expression | Value it produces | Effect on i |
|---|---|---|
++i | new value (after +1) | i increases by 1 |
i++ | old value (before +1) | i increases by 1 |
--i | new value (after -1) | i decreases by 1 |
i-- | old value (before -1) | i decreases by 1 |
Related Articles
- C++ Loops Tutorial — where ++ and — live
- C++ Range-Based For Loop — loop without a counter at all
- C++ Iterators Explained — why ++it beats it++
- C++ Variables and Data Types — the values you’re incrementing
- C++ Nested Loops — multiple counters in action
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.