Assignment_Operators_in_Cpp_New
Assignment_Operators_in_Cpp_New
2. += : Adds the right operand to the left operand and assigns the result.
Example: x += 3; is equivalent to x = x + 3.
3. -= : Subtracts the right operand from the left operand and assigns the result.
Example: x -= 2; is equivalent to x = x - 2.
4. *= : Multiplies the left operand by the right operand and assigns the result.
Example: x *= 4; is equivalent to x = x * 4.
5. /= : Divides the left operand by the right operand and assigns the result.
Example: x /= 2; is equivalent to x = x / 2.
6. %= : Computes the remainder when dividing the left operand by the right operand and
assigns the result.
Example: x %= 3; is equivalent to x = x % 3.
#include <iostream>
using namespace std;
int main() {
int x = 10;
// Basic assignment
cout << "Initial value: " << x << endl;
// Addition assignment
x += 5;
cout << "After += 5: " << x << endl;
// Subtraction assignment
x -= 3;
cout << "After -= 3: " << x << endl;
// Multiplication assignment
x *= 2;
cout << "After *= 2: " << x << endl;
// Division assignment
x /= 4;
cout << "After /= 4: " << x << endl;
// Modulus assignment
x %= 3;
cout << "After %= 3: " << x << endl;
return 0;
}
Conclusion
Mastering assignment operators is essential for any C++ programmer. They are
fundamental tools that enhance code efficiency and clarity.