• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Operators

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain why the o/p of the following code is 1,4. I am not getting the logic:-

public class Main {
public static void main(String args[]){
int x =0;
for(int i=0; i < 2; i++){
x +=(x += ++x);
System.out.println(x);
}
}
}

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

Originally posted by Chinmay Kant:
Can anybody explain why the o/p of the following code is 1,4. I am not getting the logic:-

public class Main {
public static void main(String args[]){
int x =0;
for(int i=0; i < 2; i++){
x +=(x += ++x);
System.out.println(x);
}
}
}

Thanks in Advance



I hope this helps:

1ST LOOP: x=0
x +=(x += ++x);
- Inside parentheses: x = x + (1+x) ===> x = 0 + 1 = 1
- Outside: x = x + parentheses ===> x = 0 + 1 = 1
- x = 1

2ND LOOP: x=1
x +=(x += ++x);
- Inside parentheses: x = x + (1+x) ===> x = 1 + 2 = 3
- Outside: x = x + parentheses ===> x = 1 + 3 = 4
- x = 4

Regards,
Alex
 
Chinmay Kant
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this helps:

1ST LOOP: x=0
x +=(x += ++x);
- Inside parentheses: x = x + (1+x) ===> x = 0 + 1 = 1
- Outside: x = x + parentheses ===> x = 0 + 1 = 1
- x = 1

Thanks Alex,
But one doubt
when ++x occurs it chagnges the value of x from 0 to 1
so, when this operation is done does that value does not affect further
calculation

for eg:-
Inside parentheses ++x changes the value to 1
so, x = x +(++x) ---> x = 1 + 1 ---> x= 2
so, wo should be used further.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya Alex that's my doubt tooo........

when x++ happens does it change change value to 1....
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x +=(x += ++x);

As ++x is part of the expression, the value of x will not be modified. When the controller comes to ++x, it evaluates to increment the "VALUE" of x and replaces that value as follows,

x += (x += 1) //if x=0

Hope you got it.
 
Arun Kumar Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks SrinivasaRao Madugula.........if is that the case....thanks for your answer......
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah if we consider that x still retains its previous value even after ++x the answer follows.....but the problem is why is the old value of x still retained in the expression.....anybody please elaborate...
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic