Help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help

public class Program { public static void main(String[] args) { int x = 1; x+=x++; System.out.println(x); } } why Answer is 2? why not 3 ? when I get answer 3 ?

5th Mar 2019, 1:46 PM
Iftakher Hossain
Iftakher Hossain - avatar
11 Answers
+ 10
This is a different case than the original question, in this code you are evaluating/comparing values of <x> & <y> rather than adding the variable values to themselves. Let's review the code ... int x = 1, y = 2; if(x++>y && ++y>0) Is <x> greater than <y>? no (false), being false, the next operand `++y>0` will not be checked, because of the `&&` (AND) operator. If the first operand evaluates to false then there's no point checking the next. The check fails; and execution flow moves into the "else" block where you print out the <x> and <y>. In the "else" block, value of <x> has been incremented and became 2, even though the evaluation in "if" block failed, the changes by increment operator still takes effect, because the execution flow had past that line with "if". That is how you see 22 as output. Hth, cmiiw
5th Mar 2019, 5:07 PM
Ipang
+ 9
You're welcome Iftakher Hossain In the line `x += x++;` (post increment) the value of <x> is given to `+=` operator, and added to <x> itself. The value modification by increment operator will take effect in the next line. However, if you use `x += ++x;` (pre increment) then value of <x> is incremented first, after that, it is given to `+=` operator, to be added to <x> variable itself. Using pre increment means the value is changed on that very line, not waiting for the next line to be processed by computer. Hope that clears up a bit more, feel free to ask for any doubt 👍 P.S. I'm not sure what you mean by the stored value, kindly explain ...
5th Mar 2019, 4:27 PM
Ipang
+ 8
You get 2 as an output because in the following line: `x += x++;` The value of variable <x> is not yet incremented (it's value is still 1). So you were practically adding 1 to <x>. If you wish for the value incremented before it is added then you should do like this: `x += ++x;` This way you will get 3 as an output. P.S. Next time, don't write in answers section, people may think you have solution already seeing your thread had a respond. Hth, cmiiw
5th Mar 2019, 2:21 PM
Ipang
+ 8
You are very welcome Iftakher Hossain But I'm not sure I understand very well what you meant by "How I got the effect of post increment ..." can you clarify that? English isn't my native language BTW ...
5th Mar 2019, 5:22 PM
Ipang
+ 3
Because it is being evaluated in the following way: x+=x++ => x=x+(x++) ......= 1+(1) // and incremented value of x to 2 Then evaluate the expression as 2 and assigning 2 to x
5th Mar 2019, 3:09 PM
Sinjini Das
Sinjini Das - avatar
+ 2
Ipang Thanks. but, if x=1,when i use x++, it back 1 but store its value 2 for post increment .isn't it? Than how i get the stored value of x.
5th Mar 2019, 4:09 PM
Iftakher Hossain
Iftakher Hossain - avatar
+ 2
Ipang@ public class Program { public static void main(String[] args) { int x = 1, y= 2; if(x++>y && ++y>0){ System.out.println(1); }else{ System.out.println(x +""+y); } } } what is the out put.22. Here I got the value of x=2. in the previous why not? can I understood you?
5th Mar 2019, 4:34 PM
Iftakher Hossain
Iftakher Hossain - avatar
+ 2
Many many thanks Ipang.I know This is different. But I want to say you that How i got the effect of post increment (x) in my first code?
5th Mar 2019, 5:14 PM
Iftakher Hossain
Iftakher Hossain - avatar
+ 2
Ipang. Its ok. thanks.
5th Mar 2019, 5:58 PM
Iftakher Hossain
Iftakher Hossain - avatar