aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubts getting exponentially INCREMENTed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubts getting exponentially INCREMENTed" Watch "doubts getting exponentially INCREMENTed" New topic
Author

doubts getting exponentially INCREMENTed

niraj singh
Ranch Hand

Joined: Feb 07, 2001
Posts: 36
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).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You are correct in that the system has a working area that holds what it knows values to be. This is called the scratchpad area or transaction area in some languages. If the variable x=5 then a working area is created for x's value when it is in scope. When the variable goes out of scope the contents of the working area is lost.

When you do x-- you are saying use the value of x for the variable and decrement the working area value for x for the next use. When you say --x you are saying decrement the working area and then assign that value back to x.
Let me tackle the second one first. x=5
(x - (--x))
5 - (now 4) = 1
(x--)- x
5 - (now 4) = 1
working area holds 4
(x--+--x);
5 + (now 3) = 8
4
I got lost on what you were doing for the last part, you have 2 expressions with one result?


"JavaRanch, where the deer and the Certified play" - David O'Meara
niraj singh
Ranch Hand

Joined: Feb 07, 2001
Posts: 36
Hi Cindy,
Thanks a lot for clearing up my concepts. My main doubt was regarding the precedence of () and ++, that is whether the parantheses had higer precedence than the ++ operator. The answer to that is that I was confusing precedence with associativity. () is only for associativity. The operands will still have to be evaluated left to right.
Niraj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubts getting exponentially INCREMENTed