Assignment operators (Assignment (=) and compound assignments (+=, -+, *=, /=, %=)) are used to assign the value or an expression's result to the left side variable, following are the set of assignment operators,
- "=" – it is used to assign value or an expression's result to the left side variable
- "+=" – it is used to add second operand to the existing operand's value and assigns it back (a+=b is equal to a=a+b)
- "-=" – it is used to subtract second operand from the existing operand's value and assigns it back (a-=b is equal to a=a-b)
- "/=" – it is used to divide second operand from the existing operand's value and assigns it back (a/=b is equal to a=a+b)
- "*=" – it is used to multiply second operand with the existing operand's value and assigns it back (a*=b is equal to a=a*b)
- "%=" – it is used to get the remainder by dividing second operand with the existing operand's value and assigns it back (a%=b is equal to a=a%b)
Example:
Input:
int a = 10;
int b = 3;
//operations & outputs
a = 100; //value of a will be 100
a += b; //value of a will be 103
a -= b; //value of a will be 100
a *= b; //value of a will be 300
a /= b; //value of a will be 100
a %= b; //value of a will be 1
C# code to demonstrate example of assignment operators
Output