• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

++ confusion

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output : 4
a value must be increased from 4 to 5 , but why it is not increased ???
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a++ returns the old value, which is 2, and the a on the left is evaluated before the a++ is performed, so all it does is a = 2 + 2.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Because a++ returns the old value, which is 2, and the a on the left is evaluated before the a++ is performed, so all it does is a = 2 + 2



@Stephan
I agree that a++ returns old value. statement will be equal to 2+2. What about post increment of variable a. value of variable a should get incremented after evaluation of expression right. Why is it not happening in this case?
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the proposed program:


The program shown above behaves very much like this one, which clarifies what's happening:


The variable 'a' starts with a value of 2: int a = 2. In the line 'a += a++', the value of 'a' (namely, 2) is assigned additively to the existing value of the variable 'a' (namely, 2), for a new total value of 4. Note that at this point, the assignment of the expression on the right to the variable on the left has already taken place.

Next, the expression on the right is further evaluated (changing from 2 to 3) and then thrown away. Why is it thrown away? Because the assignment of the righthand expression to the lefthand variable has already taken place and there's nowhere else to put the value '3'.

Finally, the current value of the variable 'a' (namely, 4) is printed: 'System.out.println(a)'.

The underlying principle is that there can be only one assignment of value per = operator per expression. Once that condition is satisfied and it's time to increment 'a', there's no place to put the result.
 
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public class PlusEquals {
public static void main(String args[]) {
int a = 2;


a = a++; //

System.out.println(a);
}
}

Here the result is 2 not 3.
Remember that a++ or ++a both perform first, difference is that a++ returns the previous value so in this case a++ will increase the value of a by 1 means a++ (a=a+1) is 3 but the it returns the previous value which is 2 so 3 is overwrite by 2 and final result is 2.
 
gaurav gupta sitm
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David .....
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If you are still questioning the incrementing and decrementing operators, I wrote a little guide on them which may be of interest
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step by step process to evaluate the corresponding expression........
int a=2;
a+=a++;

a+=(a++); //value of a is 2
a=a+(a++); //value of a is 2
//remeber the evaluation of expression is from left to right
a=2+(a++); //value of a is 2
a=2+(2); //value of a is 3 as b=a++; then b=2 and a=3
a=2+2;
a=4

The above process is in java as evaluation of expression is left to right



But in some languages like c,c++ the evaluation of expression is from right to leftthen the output would be 5 as follows
a+=a++;
a=a+(a++); //value of a=2
a=a+(2);//value of a=3
a=3+2;
a=5;

in case of assemblers
a+=a++;
a=a+a++;
a=a+a+1;
a=2+2+1;
a=5;


So the output is 4 in case of Java or .Net and the output is 5 with C,C++ -----
 
reply
    Bookmark Topic Watch Topic
  • New Topic