Refers to the use of the shorthand ++ incrementer or -- decrementer, either before (Prefix) or after (Postfix) the variable it is being applied to.
Prefix: ++myVar //increment, then use
Postfix: myVar++ //use, then increment
Prefix will increment the variable before it is used
Postfix will use the variable before it is incremented, then increment the variable
If used on their own, the result is identical. It is only when combined with other expressions that differences appear:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std ; int main () { int a, b, myVar = 5 ; cout << "myVar starts off as: " << myVar << endl ; a = ++myVar ; cout << "Prefixing myVar makes a: " << a << endl ; cout << "myVar is now: " << myVar << endl ; b = myVar++ ; cout << "Postfixing myVar makes b: " << b << endl ; cout << "myVar is now: " << myVar << endl ; return 0; } |
Compile & run:
myVar starts off as: 5 Prefixing myVar makes a: 6 myVar is now: 6 Postfixing myVar makes b: 6 myVar is now: 7 |