+ 1

Usage of ++prefix and postfix++

Can someone explain me why in the first case the change that a postfix makes is visible in the console (the result is 8 instead of 7) but in the second case it is not visible (the result is still 3)? https://code.sololearn.com/cKMQKnTFUQp7/?ref=app

6th Jun 2020, 8:47 AM
Devoves
2 odpowiedzi
+ 1
1st case - j = ++i + i++; j = 4 + 4; In the above case ++i has modified the value of 3 to 4 but i++ has no effect since it is a post fix. Prefix means the value is first incremented and then the value is stored in the variable assigned but where as in postfix the value is first assigned and then it is incremented.
6th Jun 2020, 8:59 AM
Avinesh
Avinesh - avatar
+ 2
Gosh, you're right! I didn't get it, that in i++ part the i has already a value of 4. Thanks for the explanation!
6th Jun 2020, 9:27 AM
Devoves