Saturday, April 19, 2008

Prefix and Postfix

Basir has already given a brief explanation of prefix and postfix. I will show some examples.


Prefix


count = ++tick;

increase variable tick by 1, then copies the value of tick to count

count = --tick;

decrease variable tick by 1, then copies the value of tick to count

Postfix


count = tick++;

copies the value of variable tick to count then increase tick by 1

count = tick--;

copies the value of variable tick to count then decrease tick by 1

int count =0, tick =0;

count= ++tick; //tick = 1 count = 1
count= --tick; //tick = 0 count = 0
count= tick++; //tick = 1 count = 0
count= tick--; //tick = 0 count = 1

You can also do the following;

count += total; //is same as count=count + total;
count -= total; // is count=count – total;
count *=3; // is count=count * 3;


Confused? Anyone doesn’t understand, keep your hand on your mouse and post comments. :)



No comments: