Assignment operators are used to assign values to variables. The most basic assignment operator is =. C++ also provides compound assignment operators such as +=, -=, *=, /=, and %=.
Assignment operators are used to store a value in a variable.
int age = 25;
Here, the value 25 is assigned to the variable age.
The = operator assigns the value on its right to the variable on its left.
int number;
number = 100;
After the assignment, number contains the value 100.
A variable can be declared and assigned a value in the same statement.
int marks = 85;
double price = 99.50;
char grade = 'A';
The value stored in a variable can be changed using the assignment operator.
int age = 20;
age = 25;
The final value of age is 25.
The value of one variable can be assigned to another variable.
int a = 50;
int b;
b = a;
Now both a and b contain the value 50.
An expression can also be assigned to a variable.
int a = 10;
int b = 20;
int sum = a + b;
The value of sum is 30.
The += operator adds a value to the existing value of a variable and stores the result in the same variable.
int number = 10;
number += 5;
This is equivalent to:
number = number + 5;
The final value is 15.
int marks = 60;
int bonus = 10;
marks += bonus;
The final value of marks is 70.
The -= operator subtracts a value from the current value of a variable.
int number = 20;
number -= 5;
This is equivalent to:
number = number - 5;
The final value is 15.
int balance = 1000;
balance -= 250;
The final balance is 750.
The *= operator multiplies the current value by another value and stores the result.
int number = 10;
number *= 5;
This is equivalent to:
number = number * 5;
The final value is 50.
int price = 100;
price *= 3;
The final value of price is 300.
The /= operator divides the current value by another value and stores the result.
int number = 100;
number /= 5;
This is equivalent to:
number = number / 5;
The final value is 20.
double price = 100.0;
price /= 4;
The final value of price is 25.0.
The %= operator calculates the remainder and stores it in the same variable.
int number = 17;
number %= 5;
This is equivalent to:
number = number % 5;
The final value is 2.
int number = 25;
number %= 4;
25 divided by 4 leaves a remainder of 1, so the final value of number is 1.
The &= operator performs a bitwise AND operation and assigns the result back to the variable.
int a = 6;
a &= 3;
This is equivalent to:
a = a & 3;
The |= operator performs a bitwise OR operation and assigns the result back to the variable.
int a = 6;
a |= 3;
This is equivalent to:
a = a | 3;
The ^= operator performs a bitwise XOR operation and assigns the result back to the variable.
int a = 6;
a ^= 3;
This is equivalent to:
a = a ^ 3;
The <<= operator shifts the bits of a value to the left and assigns the result back to the variable.
int number = 3;
number <<= 2;
This is equivalent to:
number = number << 2;
The >>= operator shifts the bits of a value to the right and assigns the result back to the variable.
int number = 20;
number >>= 2;
This is equivalent to:
number = number >> 2;
The following are common compound assignment operators in C++:
| Operator | Example | Equivalent Expression |
|---|---|---|
| += | a += b |
a = a + b |
| -= | a -= b |
a = a - b |
| *= | a *= b |
a = a * b |
| /= | a /= b |
a = a / b |
| %= | a %= b |
a = a % b |
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
number += 10;
std::cout << "After addition: " << number;
return 0;
}
The program adds 10 to the number entered by the user.
Several assignment operations can be performed one after another.
int number = 100;
number += 20;
number -= 10;
number *= 2;
number /= 5;
Each operation changes the current value of number.
Assignment can involve different numeric types, but conversions may occur.
double price = 99.75;
int amount = price;
The conversion from double to int discards the fractional part, so amount becomes 99.
#include <iostream>
int main() {
int marks = 60;
marks += 10;
marks -= 5;
std::cout << "Final Marks: " << marks;
return 0;
}
First 10 marks are added and then 5 marks are subtracted. The final value is 65.
#include <iostream>
int main() {
double amount = 1000;
amount += 500;
amount -= 200;
amount *= 2;
amount /= 2;
std::cout << "Final Amount: " << amount;
return 0;
}
#include <iostream>
int main() {
int number = 20;
std::cout << "Initial: "
<< number << std::endl;
number += 10;
std::cout << "After += 10: "
<< number << std::endl;
number -= 5;
std::cout << "After -= 5: "
<< number << std::endl;
number *= 2;
std::cout << "After *= 2: "
<< number << std::endl;
number /= 5;
std::cout << "After /= 5: "
<< number << std::endl;
number %= 3;
std::cout << "After %= 3: "
<< number << std::endl;
return 0;
}
| Operator | Name | Example | Equivalent |
|---|---|---|---|
| = | Assignment | a = b |
Assign b to a |
| += | Addition assignment | a += b |
a = a + b |
| -= | Subtraction assignment | a -= b |
a = a - b |
| *= | Multiplication assignment | a *= b |
a = a * b |
| /= | Division assignment | a /= b |
a = a / b |
| %= | Modulus assignment | a %= b |
a = a % b |
| &= | Bitwise AND assignment | a &= b |
a = a & b |
| |= | Bitwise OR assignment | a |= b |
a = a | b |
| ^= | Bitwise XOR assignment | a ^= b |
a = a ^ b |
| <<= | Left shift assignment | a <<= b |
a = a << b |
| >>= | Right shift assignment | a >>= b |
a = a >> b |
Question: Which operator adds a value to an existing variable and assigns the result back to that variable?