C++ Compound Assignment Operators
www.igiftidea.com
C++ provides compound assignment operators that combine an arithmetic or bitwise operation with an assignment operation. They provide a shorthand way to perform an operation on a variable and assign the result back to the same variable. Here are the compound assignment operators:
- Addition and Assignment: +=
x += y; // equivalent to x = x + y;
- Subtraction and Assignment: -=
x -= y; // equivalent to x = x - y;
- Multiplication and Assignment: *=
x *= y; // equivalent to x = x * y;
- Division and Assignment: /=
x /= y; // equivalent to x = x / y;
- Modulus and Assignment: %=
x %= y; // equivalent to x = x % y;
- Bitwise AND and Assignment: &=
x &= y; // equivalent to x = x & y;
- Bitwise OR and Assignment: |=
x |= y; // equivalent to x = x | y;
- Bitwise XOR and Assignment: ^=
x ^= y; // equivalent to x = x ^ y;
- Left Shift and Assignment: <<=
x <<= y; // equivalent to x = x << y;
- Right Shift and Assignment: >>=
x >>= y; // equivalent to x = x >> y;
In summary, compound assignment operators provide a shorthand way to perform an arithmetic or bitwise operation and assign the result back to the same variable. They can help to make code shorter and more readable.