Home > C++ > Operators in C++

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+3 result will be 5.
     10+20 result will be 30.
     It can be used as 10+20+30. In this case it will add 10 & 20 then the result will be added 
     to 30. Result will be 60.

– Subtract: –
This operator is used for subtraction of two values. It will subtract second operand from first operand.
E.g.

     5-3 result will be 2.
     10-20 result will be -10.
     It can be used as 10-20-30. In this case it will subtract 20 from 10 and then 30 will be
     subtracted from result. Final result will be -40.

* Multiply: –
This operator is used to multiply two values.

     2*3 will give us 6.
     15*2 will give us 30.
     10*20*30 will give us 6000. It will first multiply 10 & 20 and then result will be multiplied 
     to 30.

/ Divide: –
This operator is used to divide two numbers. It will divide the first operand by 2nd operand.
E.g.

     10/5 will give us 2. 
     5/10 will give us 0. 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/ 5 will give us 0. 
     While 5/10 will give us 5.

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:

    1. 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.

 

    1. 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:

    1. 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.

 

    1. 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();
}

Output:
operators

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 == y will return false  as values for x and y are different.
     while x == z will return true as 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 != y will return true as values for x and y are different.
     while x != z will return false   as 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 > y will return false as values for x is less than y.
     while y > z will return true as 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 < y will return true as values for x is less than y.
     while y < z will return false as 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 >= y will return false as values for x is less than y.
     while y >= z will return true value for y is greater than z and x >= z will also return 
     true as 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 <= z will return false as values for y is not less than or equal to z.
     while x <= y will return true as value for x is less than y and x <= z will also return 
     true as 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 && y will return false.
     while y && z will return true.

|| 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 || y will return true similarly y || z will also return true.
     On the other hand, x || z will return false.

! 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 as x = 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 as x = 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 as x = 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 as x = 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 as x = 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();
}

Output: –
Types of operators in c++ - arithmetic operators

One thought on “Operators in C++

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.