C++20 sections · 1024 units
Open in Course

Pre vs Post Increment

Timing of the change

Prefix ++x increments before using the value. Postfix x++ uses the value then increments. In isolation, both produce the same result: x increases by one. The difference appears in expressions.

int a = 5; int b = a++; assigns 5 to b, then increments a to 6. int c = ++a; increments a to 7, then assigns 7 to c. Prefer prefix in loops: for (int i = 0; i < 10; ++i).

Both work, but prefix avoids creating a temporary copy of the old value.