The value of the postfix increment expression is the value of the variable before the new value is stored.
Originally posted by Valentin Crettaz:
Moreover, what is the advantage of writing i=i++ when only by doing i++ i's value would be incremented?[/QB]
the most important thing here is that i++ evaluates to the value of i BEFORE the incrementation occurs.
So when evaluating i++, the current value of i (0) is stored somewhere (not relevant here), then the incrmetation proceeds (at which time i is 1) and then the assignment finally occurs, which has the effect of assigning the stored value (0) to i again. That's why i will always be 0.
The value of the prefix increment expression is the value of the variable after the new value is stored.
Originally posted by Rob Ross:
Hey Valentin, is i++ and ++i an atomic operation? Ie, thread-safe?
Won't you be my neighbor? - Fred Rogers. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton