• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

doubt on ++ operator

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

See this code:


int tricky =0;
for(int i =0;i<3;i++)
{
tricky += tricky++;
}
System.out.println(tricky);


Ans-0
Why? can you please tell me how it works.
tricky++ should return 1 and then it should calculate like this
tricky = tricky +1=1+1=2 and so on....

Please clarify this.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int tricky =0;
for(int i =0;i<3;i++)
{
tricky += tricky++;
}
System.out.println("its"+tricky);
-----------------------------
The postfix operator works after we use the variable
in the above case
when it enters in the loop for the first time
value of tricky=0
now
doing this
tricky+=tricky++;
the value of tricky would be 0//1
than i is incremented
and it enters the loop for the second time
now remember the value of tricky=0 since we have assigned the value to it see 1
than again this line gets executed
tricky+=tricky++;
the value of tricky again is assigned to zero since the postfix operator works after we use the variable
similarly for the third time it enters with the same value of tricky ie 0
-------------------------
You can try out this code to know the difference between postfix and prefix

int trickypost =0;
int trickypre=0;
for(int i =0;i<3;i++)
{
trickypre += ++trickypre;
trickypost += trickypost++;

}
System.out.println("its"+trickypre);
System.out.println("its"+trickypost);
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt understand what he said.

for it to still be zero it is nerer updated at all.

I would have thought 0,1,2
 
Joe Wolfe
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x += y;

really means x = x + y;

so tricky += tricky++;

means tricky = tricky + tricky++;

substituting in zero on the right you get

tricky = 0 + 0++;

since ++ updates after, this expression is always

tricky = 0;
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is really tricky
but this is how it works...

in each time tricky goes through the for loop,
tricky is alway tricky = 0 + 0.

therefore the value of tricky will
always be zero no matter how many times it runs through the for loop.

I hope this throws some light.

Sen Aff.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanations are not clear for me.

The very first time it enters the loop, tricky will be 0. Because of the post-increment operator, after the line tricky += tricky++, the value of tricky should be 1 before it enters the loop for the second time.

tricky++ translates to tricky = tricky + 1.

not sure why it is zero all the way. It acts as if tricky++ doesn't exist.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Krishnan:
The explanations are not clear for me.

The very first time it enters the loop, tricky will be 0. Because of the post-increment operator, after the line tricky += tricky++, the value of tricky should be 1 before it enters the loop for the second time.

tricky++ translates to tricky = tricky + 1.

not sure why it is zero all the way. It acts as if tricky++ doesn't exist.



Basically, the post increment is done after the expression is evaluated, but *before* the assignment is made. So...

The value to be assigned is calculated to be zero (tricky + tricky). The value of tricky is incremented to one. And finally, the value of tricky is assign the value of the expression, which is zero.

Henry
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>but *before* the assignment is made

The expression evaluates to zero and before this zero gets assigned to the variable it gets incremented temporarily to 1. The assignment overwrites the postincrement. Is that correct?
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry for the nice explanation.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic