+ 1
This hurts my brain
please would somebody do a rundown of whats happening here? int x = 4; int y =9; x= (y % x != 0) ? y / x : y; I have (almost) no idea whats happening. x=2 when its finished but I dont understand how.
3 odpowiedzi
+ 5
the code above is shorter way of this
int x=4;int y=9;
if(y%x!=0)
x=y/x;
else
x=y;
+ 2
Look at the order of operations to evaluate what x is.
if y % x is not equal to 0 (which returns true) then evaluate y / x which equals 2. The last part : y is essentially an 'else' statement that's evaluated if y % x evaluates false.
So if it were re-written as y % x == 0 instead of y % x != 0 then x would equal 9 since the else statement would evaluate the value of y which is 9.
+ 1
it is simple
y℅x!=0
9℅4=1 & 1!=0
so it is true
so x=y/x
x=9/4
x=2