Shimi Avizmil

Greenhorn

Posts: 16

posted 14 years ago

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 ]

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

Ranch Hand

Posts: 13974

posted 14 years ago

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.

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

Posts: 16

posted 14 years ago

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

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

Ranch Hand

Posts: 13974

posted 14 years ago

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?

result = result + (i * (j-1) );

This program prints 6:

Could you post a link to the question?

Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog

Kathy Sierra

Cowgirl and Author

Rancher

Rancher

Posts: 1589

5

posted 14 years ago

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

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 Head First Design Patterns

Just a Jini girl living in a J2EE world.

Don't get me started about those stupid light bulbs. |