In this lesson, you will learn about the Assignment Operators in C++, their usage, and examples to better understand the topic.
Assignment operators are used in C++ to give variables values. The following types of assignment operators are available in C++’s built-in library.
Operator | Example | Equivalent to |
---|---|---|
= | var1= var2; | var1= var2; |
+= | var1+= var2; | var1= var1+ var2; |
-= | var1-= var2; | var1= var1- var2; |
*= | var1*= var2; | var1= var1* var2; |
/= | var1/= var2; | var1= var1/ var2; |
%= | var1%= var2; | var1= var1% var2; |
Example
#include <iostream> using namespace std; int main() { // 90 is assigned to var1 and 10 is assigned to var2 int var1 = 90, var2 = 10; cout << "var1= " << var1 << endl; cout << "var2= " << var2 << endl; cout << "After var1*= var2;" << endl; var1 *= var2; // assigning the product of var1 and var2 to var1 cout << "var1= " << var1 << endl; return 0; }
Output
var1= 90 var2= 10 After var1*= var2; var1= 900
This concludes the C++ Assignment Operators lesson. In the next lesson, you will learn about Relational Operators in C++.