0
Increment and decrement simplified
i dont get how post fix and pre fix works pls explain :(
2 Antworten
+ 4
++n increments n, then evaluates it (pre-inctementation).
n++ evaluates n, then increments it (post-incrementation).
They are both equivalent to n=n+1 by themselves, the difference is when they are used in an expression.
int a = 1;
int b = ++a;
cout << a << endl; //prints 2
cout << b << endl; //prints 2
a = 1;
b = a++;
cout << a << endl; //prints 2
cout << b << endl; //prints 1
+ 2
• ++i means “increment i immediately,”
while
i++ means “use the old value of use the old value of i for now, but for now, but
increment i later.”