We have different types of operators in c++. These operators helps programmers in performing different task. As each of these are used for special purpose. These include Arithmetic operators, Unary (Single operand) & Binary (2 operands) operators, Relational operators, Logical operators, Assignment operators.

## Types of operators in c++

**Arithmetic**

**Binary (2 operands): –**

As name suggest binary operators are those that need two operands to perform their action.

Some binary operators are:

**+**for addition**–**for subtraction*****for multiplication**/**for division**%**for modulus

**+ Add: –**

This operator is used to add two values.

E.g.

2+3result will be5.10+20result will be30. It can be used as10+20+30. In this case it will add10 & 20then the result will be added to30. Result will be60.

**– Subtract: –**

This operator is used for subtraction of two values. It will subtract second operand from first operand.

E.g.

5-3result will be2.10-20result will be-10. It can be used as10-20-30. In this case it will subtract20 from 10and then30will be subtracted from result. Final result will be-40.

*** Multiply: –**

This operator is used to multiply two values.

2*3will give us6.15*2will give us30.10*20*30will give us6000. It will first multiply10 & 20and then result will be multiplied to30.

**/ Divide: –**

This operator is used to divide two numbers. It will divide the first operand by 2nd operand.

E.g.

10/5will give us2.5/10will give us0. This is because will only return quotient.

**% Modulus: –**

This operator is used to get the remainder of two values when they are divided.

E.g.

10/ 5will give us0. While5/10will give us5.

**Unary (Single operand): –**

**++**increment**—**decrement**–**Minus

**– Minus: – **

It is used to take negative of an operand.

E.g.

Let x=10; then –x will give us -10.

**Note: – This operator is not to be confused with Binary subtraction operator. **

**++ Increment: –**

This operator is used to increase value by 1. It is of two types:

- Pre Increment

This will increase value of variable before it (Variable) is used. And is written as:

Let x = 10 and its pre increment will be ++x.

- Post Increment

This will increase value of variable after it (Variable) is used. And is written as:

Let x = 10 and its post increment will be x++.

**– – Decrement: –**

This operator is used to decrease value by 1. It is of two types:

- Pre Decrement

This will decrease value of variable before it (Variable) is used. And is written as:

Let x = 10 and its pre decrement will be - -x.

- Post Decrement

This will decrease value of variable after it (Variable) is used. And is written as:

Let x = 10 and its post decrement will be x- -.

E.g.

#include "stdafx.h" #include "iostream" using namespace std; void main() { int x = 30; int y = 20; cout << "***** Binary Arithmetic Operators *****\n"; cout << "using + operator x + y = " << x + y << endl; cout << "using - operator x - y = " << x - y << endl; cout << "using * operator x * y = " << x * y << endl; cout << "using / operator x / y = " << x / y << endl; cout << "using % operator x % y = " << x % y << endl; cout << "\n***** Unary Arithmetic Operators *****\n"; cout << "using - unary operator on x i.e. -x = " << -x << endl; cout << "using ++ operator post x++ = " << x++<< endl; cout << "using -- operator post x-- = " << x-- << endl; cout << "using ++ operator pre ++x = " << ++x << endl; cout << "using -- operator pre --x = " << --x << endl; getchar(); }

**Different other types of Operators are:**

**Relational**

**== **For equality

**!= **For not equality

** > **For greater than

** < **For less than

<strong>>= Greater than equal

**<= **Less than equal

**== Equal: –**

This operator is used to check equality of two values. i.e. it will check that whether given values are equal or not. It can return true or false for one condition but not both at the same time.

E.g.

int x=10; int y=20; int z=10;x == ywill returnfalseas values for x and y are different. whilex == zwill returntrueas values for both are same.

**!= Not equal: –**

This operator is used to check whether two values are not equal. It is the reverse of Equality operator (==) Like equality operator this can also return true or false for one condition but not both at the same time.

E.g.

int x=10; int y=20; int z=10;x != ywill returntrueas values for x and y are different. whilex != zwill returnfalseas values for both are same.

<strong>> greater than: –

This operator is used to check if operand one is greater than operand two. It can return true or false for one condition but not both at the same time.

E.g.

int x=10; int y=20; int z=10;x > ywill returnfalseas values for x is less than y. whiley > zwill returntrueas value for y is greater than z.

** < less than: –**

This operator is used to check if operand one is less than operand two. It can return true or false for one condition but not both at the same time.

E.g.

int x=10; int y=20; int z=10;x < ywill returntrueas values for x is less than y. whiley < zwill returnfalseas value for y is greater than z.

** >= Greater than equal: –**

This operator is used to check if operand one is greater than or equal to operand two. It can return true or false for one condition but not both at the same time.

E.g.

int x=10; int y=20; int z=10;x >= ywill returnfalseas values for x is less than y. whiley >= zwill returntruevalue for y is greater than z andx >= zwill also returntrueas x and z have same values.

** <= Less than equal**

This operator is used to check if operand one is less than or equal to operand two. It can return true or false for one condition but not both at the same time.

E.g.

int x=10; int y=20; int z=10;y <= zwill returnfalseas values for y is not less than or equal to z. whilex <= ywill returntrueas value for x is less than y andx <= zwill also returntrueas x and z have same values.

**Logical**

** && **Logical and

** || **Logical or

** ! **Logical not

**&& Logical AND: –**

This operator is used to take logical AND of two operands. It will return true if both the operands are non-zero. If anyone of the two operands is zero then it will return false.

E.g.

int x=0; int y=1; int z=1;x && ywill returnfalse. whiley && zwill returntrue.

**|| Logical OR: –**

This operator is used to take logical OR of two operands. It will return true if any of the operands are true i.e. non-zero. If both operands are zero then it will return false.

E.g.

int x=0; int y=1; int z=0;x || ywill returntruesimilarlyy || zwill also returntrue. On the other hand,x || zwill returnfalse.

**! Logical not: –**

This operator is used to take logical not of the operand. It will reverse the output of the operand. i.e. if operand is returning true it will make it false and vice versa.

**Note: – Relational and Logical Operators are mostly used with Conditional Statements.**

**Assignment**

** = **to assign a value

** += **adds to previous value and assign it again to same variable

** -= **subtracts from previous value and assign it again to same variable

** *= **multiplies with previous value and assign it again to same variable

** /= **divide previous value and assign it again to same variable

** %= **takes modulus and assign it to same variable

**= Assignment: –**

This operator is used to assign values. it will assign right operand to left operand.

E.g.

int x;x=10;// this will assign 10 to x. int y;y=x;//this will assign value of x to y.

**+= Add AND assignment: –**

This operator is used to add left-hand operands value with right-hand operand value and then again assign it to left-hand side operand.

E.g.

int x = 10; int y = 20;x += y;// this will set 30 to x. it is exactly same asx = x + y;

**-= Subtract AND assignment: –**

This operator is used to subtract right hand operands value from left hand operand value and then again assign it to left hand side operand. E.g.

int x = 10; int y = 20;x -= y;// this will set -10 to x. it is exactly same asx = x - y;

***= Multiply AND assignment: – **

This operator is used to multiply left-hand operands value with right-hand operand value and then again assign it to left-hand side operand.

E.g.

int x = 10; int y = 20;x *= y;// this will set 200 to x. it is exactly same asx = x * y;

** /= Divide AND assignment: –**

This operator is used to divide left-hand operands value with right-hand operand value and then again assign it to left-hand side operand.

E.g.

int x = 10; int y = 20;x /= 20;// this will set 0 to x. it is exactly same asx = x / y;

** %= Modulus AND assignment: –**

This operator is used to take modulus left-hand operands value with right-hand operand value and then again assign it to left-hand side operand.

E.g.

int x = 10; int y = 20;x %= y;// this will set 10 to x. it is exactly same asx = x % y;

E.g.

#include "stdafx.h" #include "iostream" using namespace std; void main() { int x; int y = 20; int z = 10; x = z; cout << "***** Arithmetic Assignment Operators *****\n"; cout << "using = operator x = z : " << x << endl; x += y; cout << "using += operator x += y : " << x << endl; x -= y; cout << "using -= operator x -= y : " << x << endl; x *= y; cout << "using *= operator x *= y : " << x << endl; x /= y; cout << "using /= operator x /= y : " << x << endl; x %= y; cout << "using %= operator x %= y : " << x << endl; getchar(); }

## One thought on “Operators in C++”