Prefix / Postfix

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:

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

Leave a Reply