The difference between the two operators is a subtle but very important one. Hopefully this post clears it up.
This is something which I used to struggle with when I got into programming. The different between i++ and ++i. I would always use the wrong one. It’s actually pretty simple, but for me it took a real life example to figure it out. So I’ve created one today in the hope at least 1 person will benefit. It’s written in JavaScript, but ++i and i++ work the same in pretty much all “mainstream” languages I’ve come across.
Essentially, ++i pre-increments. That is, it increments i before using it. And i++ is a post-increment, i is used and then incremented. If you run the code below, which is a small snippet I threw together on JSFiddle.net, it should make sense: http://jsfiddle.net/4kVRQ/2/
At first i starts as 0, and then we perform ++i. This increments i to 1, before outputting it onto the page. We then perform i++. This outputs i (which is still 1) and then increments it by 1. This gives us our final answer of 2.
And of course, i— & —i work in exactly the same way, just decrementing instead of incrementing.