answersLogoWhite

0

++p increments p by 1 unit and returns the result. This is known as pre-increment.

int p = 0;

int q = ++p; // q=1, p=1.

This is effectively the same as saying:

int p = 0;

p = p + 1; // p= 1.

int q = p; // q = 1.

p++ also increments p by 1 unit, but returns the previous value of p, not the current value. This is known as post-increment.

int p = 0;

int q = p++; // q=0, p=1.

This is the same as saying:

int p = 0;

int q = p; // q = 0.

p = p + 1; // p = 1.

Of the two forms, ++p is marginally quicker because p++ employs a temporary variable for the return value, whereas ++p does not. As such, ++p is the preferred form for looping purposes:

for( int p=0; p<100; ++p);

Which version you use depends upon which value you want returned from the operator, whether it is the previous value or the new value.

User Avatar

Wiki User

13y ago

What else can I help you with?