posted 14 years ago
Hi Bonny,
If x is already initialised to be x=5, then the final result of both ++x and x++ will be x=6. The thing to notice when using prefix and postfix operators is the moment when (in this case), the addition is applied.
The best way to see this is with an example. Let's take
In this case, z will equal 8. And if we were to print out the value of x, it would be 6. However, if we alter the code slightly
The result would be that z would equal 9. and x would still equal 6.
So what is happening is that in the first code snipett, the value of x is being used, then after it has finished being used, it is incremented by 1, whereas in the second code snippet, the x is being incremented by 1 first, and then the value of z is calculated with the newly incremented x.
Where this may become confusing is when you see code such as
of
Don't let it fool you, it's result is the same. As there are no other operations that occur on that same line, all these are doing are incrementing x by 1.
Hope this helps.
Regards
Matt
[ June 20, 2006: Message edited by: Matt Gaunt ]
[ June 20, 2006: Message edited by: Matt Gaunt ]