I was recently tracing a line of code:
x -= 353 - 350
However, the answer came up to be -3.
To my surprise, I figure the -= operator would follow as:
x = x - 353 - 350
which then would equal to -703
Why is the actual answer -3
and not -703
?
I was searching for reference on this website: http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/operators.html
The example it gave led me to believe that the operator -= should produce -703.
var x:uint = 5; x -= 5; // x is now 0
Wouldn't the above example represent how x = x - 5
is 0
? Or is there an alternative code/logic that I'm missing?
Operator precendece:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
-=
has precedence of 3
, while +
and -
have precedence 13 (higher),
so the code executes as the equivalent of
x -= (353 - 350)
x -= (3)
x -= 3;
x = x - 3;
And as per basic mathematics:
x -= (353 - 350)
x += -(353 - 350)
x += (-353 + 350)
x += (-3);