Originally posted by Seema Bhatia:
How do we evaluate this expression?
a=3 b=4 c=5 d=6 e=7
What will be the values of?
a++ - --b*c/d+e
It shows me 8 but How unable to understand?
Seema
The first thing it does is fill in the values of all of the variables, so the expression would look something like this:
3++ - --4*5/6 + 7
then it evaluates each operand of all the operators:
3 - 3*5/6 + 7 (the only change here was the --4)
then it will perform the arithmetic according to the precedence rules and moving from left to right. So the first thing it does is the 3 * 5, then divides that result by 6. The result is 2.5, because the it is going to be used to evaluate another expression the .5 is truncated from it. then the rest of the original expression is finished. the result of 3-2 is added to 7. This gives the answer of 8.
I hope that helped
Dave
the precedence rules are listed here if that was the cause of your question...
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html I don't have a link on the part where the 2.5 is truncated, I'll take a look for it but if someone else has it I'm sure they'll post it.