This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi all, This is taken from Sun's free online example tests, their explenation was <b>"Resolves to (i * (j-1) ) + 1" </b>. My question is how come the -- after the i is incremnting the whole equation and not just the i variable? i.e. how come it's not <b>i*(j-1) and then i+=1.<b> Hope the question is clear... Cheers Shimi [ October 12, 2002: Message edited by: Shimi Avizmil ] [ October 12, 2002: Message edited by: Shimi Avizmil ]
I don't quite get what you posted. Is the question: result += i-- * --j; or is it: i += i-- * --j; And what is the intial value of either i or result? In the second case, the decrement on i does nothing. Just like i = i++; does nothing.
In the first case, the decrement on i has no influence on result. It's a decrement after the fact.
Hi Paul, thanks for the reply. I'll try again, the code is: result += i++ * --j doesn't matter what the values are, sun's explanationwas that the above equation is the same as : result = result + (i * (j-1) ) + 1 I don't understand why the "+ 1" is applied to result and not to i? I though that "result += i++ * --j" is the same as the next 2 lines: result = result + (i* (j-1)) i=i+1; Hope it's clearer now...
Joined: May 05, 2000
That is incorrect. The equation works out to: result = result + (i * (j-1) ); This program prints 6:
Originally posted by Shimi Avizmil: doesn't matter what the values are, sun's explanationwas that the above equation is the same as : result = result + (i * (j-1) ) + 1.
Actually, the original values DO matter. The original values were: i = 3; j = 0; result = 1; <-- this is where the "+1" is from!! And Sun's response was NOT: result = result + (i * (j-1) ) + 1 It was: Resolves to (i * (j-1) ) + 1 It wasn't supposed to be interpreted as a "general formula" but rather as the result of this *particular* expression, which in this case involves an original value of "1". So the "+1" is not reflecting the post-increment operation, but rather the "1" that was the original value of result. So the question and answer are correct, but I think the answer is really confusing, and I can see how it could easily be misinterpreted. I'll see if we can add a better, more detailed explanation up there. Thanks!!