posted 14 years ago
Let’s see how the k = k / --k statement is evaluated as k = 12 / 11 statement.
Initially, k = 12.
k = k / --k, is an assignment statement so the right hand expression will be evaluated first.
Now, k / --k expression will be evaluated from left to right.
k is evaluated as 12 and the value is loaded into the processor register or into the processor cache memory (according to the processor architecture) from the physical memory (RAM).
--k is a pre-decrement and single expression. When it is evaluated, there are two things will be happened – the value of its will be decreased by 1 and a value will be returned. Since it is a pre-decrement expression so the new value will be returned, that is, 11 will be returned. So, --k is evaluated as 11 and the value is loaded into the processor register as usual.
So, the k / --k expression is evaluated as 12 / 11.
(The rest of the story, that is, the execution of the simple integer arithmetic is done by the processor and returns the value accordingly which is 1 and the value is assigned into the k variable.)
In the case of post-increment or post-decrement, the evaluation is done in the same way but return the previous value.