Assignment Operators


Assignment operators are used to assign value to variables. The function on the left side of the assignment operator is the variable on the right and right side of the assignment operator.

The different types of assignment operators are shown below:

"=": This is a straightforward assignment operator. This operator is used to share the value to the right of the variable on the left.

For example:

a = 10; b = 20; ch = 'y';

"+ =": This operator is a combination of '+' and '=' operators. The operator first adds the current value of the left variable to the right value and then gives the effect to the left variable.

Example:

(a + = b) can be written as (a = a + b) If the first value stored in a is 5. Then (a + = 6) = 11.

"- =" This operator is a combination of '-' and '=' operators. The operator starts by subtracting the current value of the left variable from the correct weight and assigning the result to the left variable.

Example:

(a - = b) can be written as (a = a - b) If the first value stored in a is 8 Then (a - = 6) = 2.

"* =" This operator is a combination of '*' and '=' operators. The operator first duplicates the current value of the left variable to the right value and then assigns the result to the left variable.

Example:

(a * = b) can be written as (a = a * b) If initially the value kept in a is 5. Then (a * = 6) = 30.

"/ =" This operator is a combination of '/' and '=' operators. The operator first divides the current variable value on the left with the right and then assigns the result to the left variable.

Example:

(a / = b) can be written as (a = a / b) If the first value kept at 6 is 6. Then (a / = 2) = 3.

Example:

#include <iostream> using namespace std; int main() { int a=110,b=20,c; c=a+b; cout<<"\n c=a+b:"<<c; a+=a; cout<<"\n a=a+a:" <<a; a*=a; cout<<"\n a=a*a:" <<a; a/=a; cout<<"\n a=a/a:" <<a; a-=a; cout<<"\n a=a-a:" <<a; return 0; }

OUTPUT:

string type