C++ Operators

You declare a variable after that operators are used to perform logical or mathematical operations on that variable’s values. For example, int age = 27; where the “=” symbol is an assignment operator that assigns 27 to the age variable.

C++ programming language support many operators that are divided into different groups.

1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators

Let’s discuss them one by one.

1. Arithmetic operators

It is a well-known group of operators to perform a mathematical operation on variables. Here is the list of arithmetic operators along with their operational effects.

Example Arithmetic operators

``````//Arathmatic operators
#include<iostream>
using namespace std;
int main()
{
int num1=8, num2=2;

cout<<"num1 + num2 = "<<num1+num2<<endl;
cout<<"num1 - num2 = "<<num1-num2<<endl;
cout<<"num1 * num2 = "<<num1*num2<<endl;
cout<<"num1 / num2 = "<<num1/num2<<endl;
cout<<"num1 % num2 = "<<num1%num2<<endl;

}``````

Output

``````num1 + num2 = 10
num1 - num2 = 6
num1 * num2 = 16
num1 / num2 = 4
num1 % num2 = 0``````

2. Assignment Operators

As its name indicated that they are used to assign value to the variables.

Let’s have look at different assignment operators with examples.

Example Assignment Operators

``````//Assignment operator
#include<iostream>
using namespace std;
int main()
{
int num1=8, num2=2;

num1+=num2;
cout<<"num1 += num2 = "<<num1<<endl;

}``````

Output

``num1 += num2 = 10``

3. Relational Operators

Relational c++ operators are also known as comparison operators used to compare two values. They check the condition of both values and give “true as 1” or “false as 0” results depending on the relational operator used.

Here is the list of relational operators with examples

Example Relational Operators

``````//Relational operator
#include<iostream>
using namespace std;
int main()
{
int num1=8, num2=2;
int result;

result = (num1 == num2);   // false
cout << "8 == 2 is " << result << endl;

result = (num1 != num2);  // true
cout << "8 != 2 is " << result << endl;

result = (num1 > num2);   // false
cout << "8 > 2 is " << result << endl;

result = (num1 < num2);   // true
cout << "8 < 2 is " << result << endl;

result = num1 >= num2;  // false
cout << "8 >= 2 is " << result << endl;

result = (num1 <= num2);  // true
cout << "8 <= 2 is " << result << endl;

}``````

Output

``````8 == 2 is 0
8 != 2 is 1
8 > 2 is 1
8 < 2 is 0
8 >= 2 is 1
8 <= 2 is 0
``````

4. Logical operators

Logical operators in c++ are used to check whether the condition is true or false. They are commonly used with conditional statements.