+ 2
Why it’s 70 and not 69?
Where is the mistake people, who can help? If x = 34 and y = ++x, then y supposed to be 35 which means x+y=69, no? When I run the code I receive an answer 70(( public class Program { public static void main (String [ ] args) { int x = 34; int y = ++x; int z = x+y; System.out.println(z); } } Thanks, Arthur
6 Réponses
+ 14
int x=34;
int y=++34 //y=35 as well as x=35
int z=35+35 = 70
//hope u got it ☺
+ 12
https://www.sololearn.com/Discuss/407846/?ref=app
this will give you some more information about increment decrement
+ 3
The statement:
int y = ++x;
increments the value of x by one and then assigns the incremented value to y.
It means that after this statement, both x and y contain the value 35.
That's why the output is 70, not 69.
If the statement was:
int y = x+1;
the output would have been the way you are thinking, because it does not increment the value of x so it remains 34 and y becomes 35.
+ 3
For z = 69, write this:
int x = 34;
int y = x++;
z = x+y;
Now y is 34 and x is 35, because x gets incremented AFTER y copies its value.
+ 1
the ++x increments x and then makes y=x
x is 35 after the ++x and not 34 anymore
+ 1
Thank you guys