100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Increment and Decrement Operators in C++

Learn the difference between prefix and postfix ++ and -- in C++, a frequent exam topic, with clear step-by-step examples.

OperatorsBeginner6 min readJul 7, 2026
Analogies

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

cpp
++x;   // prefix increment
x++;   // postfix increment
--x;   // prefix decrement
x--;   // postfix decrement

3. 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

cpp
#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

text
a++ prints: 5
a is now: 6
++b prints: 6
b is now: 6

6. Key Takeaways

  • Prefix ++x increments 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#IncrementAndDecrementOperatorsInC#Increment#Decrement#Operators#Syntax#StudyNotes#SkillVeris#ExamPrep