+ 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.

21st Jan 2017, 12:07 PM
Maximus Wallis
Maximus Wallis - avatar
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;
21st Jan 2017, 1:29 PM
M3hran
M3hran - avatar
+ 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.
31st Jan 2017, 2:31 AM
Mike
Mike - avatar
+ 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
21st Jan 2017, 12:46 PM
Megatron
Megatron - avatar