+ 16
What is the output of this code and why?
int x = 4; int y = ++x * 4 / x + x; System.out.print(y + x); Please anyone help me to shortout the problem. Thanks in advance!!!☺
65 Respostas
+ 36
++x=5 //incrementation of x by 1, x=x+1=>4+1=5
now, x=5 //x becomes 5 due to incrementation
++x*4/x=>5*4/5=4
y=++x*4/x+x
=4+5
=9
Now, y=9 and x=5
So, output will be 9+5= 14
The incremented value of x is used in place of x...
So output you'll get is 14...
+ 25
According to diagnostics, the value of y is 9. The prefix increment statement is evaluated prior to statement evaluation, hence all x in the y assignment statement is 5.
y = 5 * 4 / 5 + 5
y = 9
Final output prints (x + y), which is (5 + 9) = 14.
+ 9
@Tanjibur Because ++x increments x by 1 and store it back to x.
+ 8
++x increments value by 1 and uses it.
x++ uses it and increments value by 1.
+ 8
@vengat
...
if this continued then it will become question of the day 😂😂😂
+ 7
@Tanjibur I made a careless mistake, using the non ++ed value of x.
Answer now edited. Thanks for pointing it out
+ 6
You made me do my first java code too!
TBH I suck at Java
+ 5
sounds like your homework hahahaa
int x=4
int y=(++x=5)*4/(5)+5
y=9
x+y=5+9=14
Why not you try on code playground?
+ 5
@Tanjibur Rahman
++ is an increment operator, it increments a value by 1. The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The only difference is that the prefix version (++x) evaluates to the incremented value, whereas the postfix version (x++) evaluates to the original value.
+ 5
My advice would be to do some reading on operator precedence in Java (read the official Oracle documentation and play with code). Operators with higher precedence are evaluated before operators with relatively lower precedence.
The unary (prefix) operator has higher precedence than multiplicative operators, which in turn have higher precedence than additive operators.
That means ++x gets evaluated first. So y = 5 * 4 / 5 + 5
x is now 5 and y is 9 and your code outputs 14 ☺️
+ 5
++x just works like that. Can't say anything more.
Javascript PHP, C++,C# all use these incrementers
+ 5
Please people
I would like to end this discussion.
Answer is settled.
So is explanation.
----------CLOSE THIS QUESTION-------------
+ 4
u got y=5 *4/5 +5
☝☝☝
what will u do here( 5*4 then divide by 5 ) = 4 or (5 * 4 divide by 5) =0
this is ur main doubt ,
then buddy just see 👉precedence order👈 for doing these question
+ 4
Here ++ used before x is a prefix increment operator. It first increments the value of x by 1and then further calculation goes on.
Therefore:-
y = 5*4/5+5;
(y= 4+5=9)
Now x+y = 9+5 = 14
+ 4
ans is also 14.
++x++4=5
y=9
x+y=5+9=14
x is increment
+ 4
guys guys guys
he knows the solution now
no need to answer
+ 4
output is 14....
points that u need to remember is that
1)value gets incremented from right to left
2)During arithmetic operations Priority order of the application operators follow the rule of BODMAS
3)And of course when u are addin' up x & y for the output don't forget tp take the incremented valure of x which is 5
+ 3
Tnxx Daniel Stanciu ☺
Now I get it
+ 3
I got the result as 14 according to my calculation,
and i think lot many explanations are already given before i saw the question.