0

what is pre increment and post increment

1st Aug 2017, 1:25 PM
zachary
2 Réponses
+ 5
int yarnaika(int a){ cout << a << endl; } int main(){ int b = 2; yaranaika(b++);//b++ is post and ++b is pre /*If post it will print 2 out because you post to yaranaika() with 2*/ /*If pre it will print 3 out because b is incremented first then post value*/ cout << b; } //Well My explaintion is so bad :(
1st Aug 2017, 1:32 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
pre-increment increments the value of a variable and returns the resulting value; while post increment increments the value of the variable and returns the value prior to the increment. So, if a variable x initially contains the value 3, the following expressions yield different results:   y = ++x; // y==4, x==4   y = x++; // y==3, x==4 The effect on x is the same in both cases, but y gets a different value. This is all straightforward. The programmer needs to be careful to use the correct operator to achieve the required result in an expression.
1st Aug 2017, 1:32 PM
Eremie Gillowei
Eremie Gillowei - avatar