File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes result += i++ * --j   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 "result += i++ * --j   " Watch "result += i++ * --j   " New topic
Author

result += i++ * --j

Shimi Avizmil
Greenhorn

Joined: Oct 09, 2002
Posts: 16
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 ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Shimi Avizmil
Greenhorn

Joined: Oct 09, 2002
Posts: 16
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
That is incorrect. The equation works out to:
result = result + (i * (j-1) );
This program prints 6:

Could you post a link to the question?
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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.
Shimi Avizmil
Greenhorn

Joined: Oct 09, 2002
Posts: 16
Cheers, it's clear now...
Shimi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: result += i++ * --j