Dashboard
G
0:00 / 0:00

Increment and Decrement Operators, Prefix form and postfix form

C++ · 28m

In C++, the increment operator (++) increases a variable's value by 1.
There are two forms:
1. Prefix Increment — ++x
In prefix, the variable is increased first, then its new value is used.
int x = 5;
int y = ++x;
Result:
x = 6
y = 6
Because x becomes 6 before assigning it to y.

2. Postfix Increment — x++
In postfix, the current value is used first, then the variable is increased by 1.
int x = 5;
int y = x++;
Result:
x = 6
y = 5
Because y gets the old value 5, and then x becomes 6.
🔑 Easy way to remember
Form Example What happens first?
Prefix ++x Increment → Use value
Postfix x++ Use value → Increment
Simple example
int x = 10;

cout << ++x; // 11
cout << x++; // 11
cout << x; // 12
Remember:
Prefix = Change first
Postfix = Use first

You're watching a free preview

Enroll to unlock all lessons, chapter tests and your certificate.

Enroll in C++
Download Lesson Resource