Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

doubt regarding i = i++;

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all please help me out in this one coz i am getting a bit confused
------------------
int i = 0;
i = i++;
i = i++;
i = i++;
System.out.println(i);
-------------------
ANS is 0
but if i am writing
--------------------
int i=o,j;

j = i++;
j = i++;
j = i++;
System.out.println(i);
------------------
here i is 3
and j is 2
can any body explain why in the first code i is not incrementing
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amitkumar,

when u write i = i++; what is happening is as follow

i = i++;
i = (return i and the value if i will be incremented to 1 )
since the value returned is 0 thus 0 is assigned to i again. ( The incremented value of i is overwritten with the new value (0) )

but in case of j = i++;
j = (return i and the value of i is incremented to 1 )
the value of j will be that of i before increment i.e. 0 and the value of i will increment to 1 ( In this case the value of i is not overwritten but assigned to j)

I hope u got it.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case of j=i++;
it is equivalent to
j = i;
i++;

but in case of i=i++;
Why is it not equivalent to
i = i;
i++;
 
Manish Nijhawan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case of i = i++;

i = ( return i and increment the value of i by 1)

initially i is 0 , the value to be returned is determined to be 0. then the value of i is incremented by 1 so i become 1 now the return happens which return the value 0.

the return happens after the value of i is incremented.

the defination of i++ is :- the current value of i is to be taken for evaluating the expression and after using its current value increment it by 1

the value of i is incremented before the returns happens

consider the following example
int i = 0 , j ;
j = i++ + i++;
the answer will be j = 1 and i = 2 and not j = 0 , this is because after using the value of i , it is incremented immediately so that the new value can be used in the next increment. after the whole expression is evaluated and the increment being done , then the value of the expression is returned to the left hand side variable.

I hope that i have made myself clear.
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx! Manish it is clear now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic