• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

result += i++ * --j

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shimi Avizmil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!
 
Shimi Avizmil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers, it's clear now...
Shimi
 
reply
    Bookmark Topic Watch Topic
  • New Topic