+ 1
Assignment Operators problem...
I can't understand this: x *= 3; // equivalent to x = x * 3 x /= 2; // equivalent to x = x / 2 x %= 4; // equivalent to x = x % 4 so "X" is a number for example 2, so the first one would be 2*=3 //equivalent to 2=2*3 right? but where's the point of doing that, im so confused please help!
5 Réponses
+ 16
When we write something like x = x+1,
Think it as, new x = old x + 1
So, x = 2+1
We are supposed to calculate/ assign the left hand side operand based on the right hand side formula.
+ 11
Don't see it the mathematical way...
How can I explain it? I'll try this:
Imagine the variable is a box, named x (ok.... I know... maybe somehow it is signed as x).
In your example you first put the number 2 in it:
x=2;
and then you take the number 2 out of it again multiply it by 3 and put the result 6 back in the box again.
x=x*3;
You can go to that box and see what's in there any time you need it.
x*=3; is just a short way to write x=x*3;
+ 6
for example:
assuming we have the variable x which is assigned the value of 3
if we got x *= 3
then we do not have the situation of 3 = 3 * 3
instead we have it like this:
x = 3*3
this seems a little bit out of world and pretty strange, but you gotta see it like that:
variable x just points to a value
with every new operation to the variable, not the variable itself is changed, but the so thought variable-arrow that points to a specific value
so to say one could understand it like this more easily:
x *= 3
--> x(new) = x(old) * 3
which is in this case:
--> x(new) = 3 * 3
so now: x = 9
doing that again with the new value:
--> x(new) = x(old) * 3
which is in this case:
--> x(new) = 9 * 3
so now: x = 27
I hope I helped you with that😝
+ 1
X's new value = X's old value * 3
+ 1
x ÷= 5