so I'm supposed to read a code and I don't understand what this notation means. I understand that n+= bla, means n+1= bla. But i can't make out the meaning of n*= -1. Can someone tell me real quick. Also does anyone know WHY this would be used instead of a clearer notation? Whoever wrote the code couldn't have possibly saved more than a few letter when using this...
n += bla
is equivalent to n = n + bla
n *= -1
is equivalent to n = n * -1
There's no functional reason to choose one notation over the other, it's just a stylistic preference. Java language (and others as well) is full of equivalent statements:
n++
is equivalent to n=n+1
n=m=1
is equivalent to n=1; m=1;
n=1; m=1;
and many many others
n*= -1
does same thing as: n = n* -1
In Java or C++, there are things like:
+=, -=, *=, /=, &=, |=
They are all the same in a sense.
That is, do the operation first, then do the assignment.
It's the compound assignment operator version of the multiplication operator.
The code n *= x
is equal to n = n * x
So in your case, n *= -1
would make n
equal to itself multiplied by -1.
The only difference between using the compound assignment operator and the normal assignment operator with a multiplication expression is of stylistic preference.
You can read about compound assignment operators here: http://java.about.com/od/c/g/compoundassgnment.htm
It means n = n*(-1). Just changing the sign.
n += bla
is the same as n = n + bla
n *= -1
is the same as n = n * -1
All of these operators act the same way : value math-operator assignment-operator other value
is the same as value assignment-operator value math-operator other value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.