In this tutorial, we will learn about many operators in C++ with the help of examples.

Operators are symbols that forms operations on variables.

Operators in C++ can be classified into 6 types:

- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Other Operators

Arthmetic operators are used to perform arthematic operations.

These operators include *****,**/**,**%**,**+** and **-**.

```
#include
```
using namespace std;
int main() {
int x, y;
x = 10;
y = 5;
// displaying the sum of x and y
cout << "x + y = " << x+y << endl;
// displaying the difference of x and y
cout << "x - y = " << x - y << endl;
// displaying the product of x and y
cout << "x * y = " << x * y << endl;
// displaying the division of x by y
cout << "x / y = " << x / y << endl;
return 0;
}

```
a + b = 15
a - b = 5
a * b = 50
a / b = 2
```

Assignment Operators are used assign the value.The assignment operator is **=**

```
//assiging value
int a=45;
```

Operator | Equvilant to |
---|---|

= | a=b; |

+= | a=a+b; |

-= | a=a-b; |

*= | a=a*b; |

%= | a=a%b; |

/= | a=a/b; |

```
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
// value of a is assigned to temp
temp = a; // temp will be 2
cout << "temp = " << temp << endl;
// assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}
```

```
temp = 2
a = 9
```

A relational operator is used to relate or compare two or more values.

```
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 3;
b = 5;
bool result;
result = (a == b); // false
cout << "3 == 5 is " << result << endl;
result = (a != b); // true
cout << "3 != 5 is " << result << endl;
result = a > b; // false
cout << "3 > 5 is " << result << endl;
result = a < b; // true
cout << "3 < 5 is " << result << endl;
result = a >= b; // false
cout << "3 >= 5 is " << result << endl;
result = a <= b; // true
cout << "3 <= 5 is " << result << endl;
return 0;
}
```

```
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1
```

Logical operators are used to check if this logic is true or false.

```
#include <iostream>
using namespace std;
int main() {
bool result;
result = (3 != 5) && (3 < 5); // true
cout << "(3 != 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 < 5); // false
cout << "(3 == 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 > 5); // false
cout << "(3 == 5) && (3 > 5) is " << result << endl;
result = (3 != 5) || (3 < 5); // true
cout << "(3 != 5) || (3 < 5) is " << result << endl;
result = (3 != 5) || (3 > 5); // true
cout << "(3 != 5) || (3 > 5) is " << result << endl;
result = (3 == 5) || (3 > 5); // false
cout << "(3 == 5) || (3 > 5) is " << result << endl;
result = !(5 == 2); // true
cout << "!(5 == 2) is " << result << endl;
result = !(5 == 5); // false
cout << "!(5 == 5) is " << result << endl;
return 0;
}
```

```
(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 < 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0
```