+ 5

Regarding pre and post increment.

can you please check my code"test" and explain the reason for difference in the output.

19th Dec 2016, 6:42 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
2 Réponses
+ 5
Yeah, for "i=1; ++i = ++i + i;" : the first prefix increment operator "++i" increments i by 1 BEFORE the expression happens. So now i == 2, but when assigning "++i + i" to i, the second "++i" increases i again by 1 BEFORE the expression happens. Now i = 3, therefore i = 3 + 3 = 6. For "j=1; ++j +=j;" : the "++j" increases j by 1 BEFORE the expression happens, so now j = 2. The expression "j+=j" or "j = j + j" is equivalent to j = 2 + 2 = 4.
19th Dec 2016, 7:08 AM
Antek
+ 1
thanks for you answer @antek you made it very clear to me.
19th Dec 2016, 7:03 AM
Rishabh Agrawal
Rishabh Agrawal - avatar