This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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...

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974

posted

0

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!!

Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.