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 “=” 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 it one by one.


1. Arithmetic operators

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

OperatorNameDescriptionExample
+AdditionUsed to add two valuesNum1+num22
SubtractionSubtract one value from the othernum1-num2
*MultiplicationMultiplying two valuesNum21*num2
/DivisionDivide two valuesnum1/num2
%ModulesUsed to get division reminderNum1%num2

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.

OperatorNameEquals to
=num1= num2Num1 = num2
+=num1+=num2num1 = num1+num2
-=num1-=num2num1 = num1-num2
*=num1*=num2num1 = num1*num2
/=num1/=num2num1 = num1/num2
%=num1%=num2num1 = num1%num2

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 operators in c++ are also known as comparison operators used to compare two values. It checks the condition of both values and gives “true as 1” or “false as 0” results depending on the relational operator used.

Here is the list of relational operators with examples

OperatorNameExample
==Equals to4==5 give result false
!=Not equal to4!=5 gives the result true
Less then4<5 give result true
Greater then4>5 gives result false
<=Less than equal to4<=5 give result false
>=Greater than equal to4>=5 give result false

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.

OperatorNameExampleDescription
&&And operator Num1 && num2True when both num1 and num2 are true otherwise give a false result
||Or operator Num1 || num2True when num1 or num2 is true else give a false result
!Not operator!numTrue when num is false