1. Intro
The increment (++) and decrement (--) operators are unary operators that add or subtract 1 from a variable. Each has two forms: **prefix** (++x, --x) and **postfix** (x++, x--). Both forms change the variable's value by 1, but they differ in what value the *expression itself* evaluates to — this is one of the most commonly tested C++ concepts.
Cricket analogy: A scorer bumping the run tally by one after a single, either announcing the new total immediately or logging the old one first before updating, mirrors the prefix versus postfix difference in ++x versus x++.
2. Syntax
++x; // prefix increment
x++; // postfix increment
--x; // prefix decrement
x--; // postfix decrement3. Explanation
**Prefix** (++x): the variable is incremented first, and the expression evaluates to the *new* (updated) value. **Postfix** (x++): the expression evaluates to the *old* (original) value first, and the increment happens after that value has been used. Both forms leave x incremented by 1 once the full statement finishes — the difference only matters when the result of the expression is used immediately, such as in cout << x++; versus cout << ++x;.
Cricket analogy: Prefix ++x is like the umpire raising the new run total instantly for the crowd to see, while postfix x++ is like the scoreboard operator using the old total for the replay graphic before updating it — both end at the same final tally.
Given int a = 5;, the statement cout << a++; prints 5 (the old value) and then a becomes 6. The statement cout << ++a; (with a reset to 5) prints 6 (the new value) directly, because the increment happens before the value is used. Mixing these up is the single most common exam mistake with ++/--.
4. Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
cout << "a++ prints: " << a++ << endl;
cout << "a is now: " << a << endl;
int b = 5;
cout << "++b prints: " << ++b << endl;
cout << "b is now: " << b << endl;
return 0;
}5. Output
a++ prints: 5
a is now: 6
++b prints: 6
b is now: 66. Key Takeaways
- Prefix
++xincrements first, then returns the new value. - Postfix
x++returns the old value first, then increments. - After either form, the variable itself ends up incremented by exactly 1.
- The distinction only matters when the expression's result is used immediately, e.g. inside
cout <<or as a function argument.
Practice what you learned
1. Given `int a = 5; cout << a++;`, what is printed?
2. Given `int b = 5; cout << ++b;`, what is printed?
3. After `int a = 5; int x = a++;`, what are the values of `a` and `x`?
4. After `int b = 5; int y = ++b;`, what are the values of `b` and `y`?
5. Does `x--` change the value of `x` by more or less than `--x`?
Was this page helpful?
You May Also Like
Arithmetic Operators in C++
Learn how C++ arithmetic operators (+, -, *, /, %) work, including integer division truncation and modulo rules, with runnable examples.
for Loop in C++
Learn the C++ `for` loop syntax, how init/condition/increment control iteration, and when it is the best choice for a known number of repetitions.
Assignment Operators in C++
Learn C++ assignment operators including =, +=, -=, *=, /=, and %=, and how compound assignment shortens common update expressions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics