You are here

Prefix Increment

10 February, 2015 - 10:46

Prefix increment says to increment me now and use my new value in any calculation. An example:

int oldest = 44; // variable set up with initialization   then later on in the code age = ++oldest; 

The variable oldest is incremented with the new value changing it from 44 to 45; then the new value is assigned to age.

In postfix age is assigned 44 in prefix age is assigned 45. One way to help remember the difference is to think of postfix as being polite (use my existing value and return to increment me after the other operators are done) where as prefix has an ego (I am important so increment me first and use my new value for the rest of the evaluations).