I have been reading a lot of discussion on pre-increment and post-increment in this forum, but still I have not been able to clearly understand the program flow logic for pre and post increments. I have attached a detailed discussion describing the way the increment operator works from the sun
java forum site.
Could someone tell me the step-by-step logic flow for a pre and post increment operator.
Question 1 - My first question is regarding the code attached below which deals with i=i++
My understanding is that the sequence is as follows -
1. The full expression to the right of the "=" operator has to be evaluted first.
2. My earlier understanding was that post-increment means the variable is incremented after complete execution of the statement and before control moves to the next statement. But here, the value of i is inserted before start of expression evaluation. So i set to 0.
3. i++ has to be evaluated before assignment because ++ operator has higher precedence. Thus i gets incremented to 1.
4. This is where I get hazy. Does he mean that the incremented value of i which is now 1, is not assigned to the i on the left hand side of the "="operator, but instead stored in a temporary memory location as 1. The original value of i is located by Java (which is 0) and is assigned to i. So, i remains at 0, but the temporary storage has a value 1. So, what happens to the temporary storage......is it just dropped and the value lost?
Now, when dealing with pre-increment, the same 4 steps are -
1. The full expression to the right of the "=" operator has to be evaluted first.
2. The value of i will be plugged in after the increment......what does it mean? How will i be incremented when it has no value.
3. A pre-increment too has higher precedence than a = operator, so ++i has to be evaluated before assignment because ++ operator has higher precedence. Thus i gets incremented to 1.
4. In the previous case, I assumed the post-incremented value is stored in a temporary memory location. Here, I assume, the difference would be that, no temporary memory location is used, but instead the actual memory location of i is updated to the value 1. The value of the expression is processed which is also 1, and this value is assigned to the variable i, which is on the left side of the = operator.
Question 2 - This relates to a question put up on this forum a few days back. I am reposting the code -
In the line marked 1, we have x---x/0. Is this expression evaluated as -
1. (x - (--x))/0 or
2. ((x--) - x)/0
Changing this, I left out the divide by zero, and tried to evaluate the remaining expression. I have taken x=5. Hence-
1. (x - (--x)) //answer is 1
2. ((x--) - x)//answer is 1
If the answer is 1, I tried to use the Question 1 logic of pre and post increment, but have failed to understand, how both evaluate to 1. Please explain logic as also relevant to Question No. 1.
If I further modify the question (keeping x = 5) to -
(x--+--x);//result is 8
But if I change it to -
((x--)-x);
(x--+--x);//result is 6
Could someone please explain the above questions.
[This message has been edited by niraj singh (edited March 09, 2001).]