In a lot of languages a = a + b can be written as a += b In case of numerical operations, a + b is same as b + a, so the single compound operator suffices.
Also, a = a - b can be written as a -=b .
However, a-b is not equal to b-a. Hence, the compound assignment operator does not work for a = b - a
So, are there compound assignment operators for the operation a = b op a (where op can be +, -, *, /, %, and order matters) ?
[Non commutative operations]
The assignment statement stores a value in a variable. Compound assignment combines assignment with another operator.
The compound-assignment operators combine the simple-assignment operator with another binary operator. Compound-assignment operators perform the operation specified by the additional operator, then assign the result to the left operand. For example, a compound-assignment expression such as. expression1 += expression2.
No there isn't a short-hand notation for a = b + a
. If you need to do a lot of a = b + a
for strings, you'd better build a list like:
lst = []
...
lst.append("a")
lst.append("bb")
lst.append("ccc")
lst.append("dddd")
...
lst.reverse()
return ''.join(lst) # "ddddcccbba"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With