Assignment Operators in C++

In this article, we are going to learn what are assignment operators and their various types in C++. Assignment operators, as the name suggests are used for assigning value to a variable.

Types of Assignment Operators in C++

Let us see some types of assignment operators in C++.

‘=’

It is used to assign the value on the right side to the variable on the left side.

Example:

x=3;

This statement assigns the value 3 to the variable x.

‘+=’

It is a combination of the ‘+’ and ‘=’ operators. This operator adds the current value of the variable on the left side to the value on the right side and then assigns the result to the variable to the left.

Example:

x+=3;

This statement is the same as x=x+3.

Similarly, the ‘+=’ operator can also be changed to ‘-=’, ‘/=’, ‘*=’ signifying subtraction, division and multiplication respectively.

Code Implementation:

#include <iostream> 
using namespace std; 

int main() 
{  
  int a = 3; 
  cout << "Value of a is "<<a<<"\n"; 

  a += 10; 
  cout << "Value of a is "<<a<<"\n";

  a -= 3; 
  cout << "Value of a is "<<a<<"\n"; 

  a *= 10; 
  cout << "Value of a is "<<a<<"\n"; 

  a /= 10; 
  cout << "Value of a is "<<a<<"\n"; 

  return 0; 
} 

Output:

Value of a is 3

Value of a is 13

Value of a is 10

Value of a is 100

Value of a is 10

I hope, you have understood how our program implements the various assignment operators in C++.

Mention below your comments, if you find any mistake in the article or to share more information related to the topic of this article.

Similarly, you may also learn,

Shift Operators in C++

Bitwise Operators in C or C++

Leave a Reply

Your email address will not be published.