Operators in Cpp

Operators in Cpp


What is operators ?

Operators are nothing but a symbol that we used to perform some operations.
 

In this tutorial you will learn :

 
 
Here we present types of operators :
 

Arithmatic Operators :

We use it when we need to solve some mathematical operations. Arithmatic operators are two types.
1. Unary Operators : 
It operates with a single operand. (e.g ++, --)
2. Binary Operators :
It works with two operand.  (e.g +, -, * , /)
 

Some Arithmatic Operators and their usage :

+ :

It is used to solve additional problems.

-- :

It is used to solve subtraction problems.

* :

It is used to solve multiplication problems.

/ :

It is used to solve division problems.

++ :

It is used as increment operator.

% :

It is used to solve modulus division problems.

Sample Program :


#include<iostream>
using namespace std;
int main() 
{
    int x = 2 , y = 4 , z; 
    z = x+y; 
    cout <<"the addition is " <<z; 
    return 0; 
}

Output :

the addition is 6

Related Program :

C++ Program to perform Arithmetic Operations

 

Relational Operators :

We use it to check the relationship between two operands.
 

Some Relational Operators and their usage :

== :

It is used for comparison. when we have to show two variables are equal.

!= :

It's used when we have to show two variables are not equal.

< :

It's used when we have to show the first variable less than the second variable.

> :

It's used when we have to show the first variable greater than the second variable.

<= :

It's used when we have to show the first variable lesser equal than the second variable. 

>= :

It's used when we have to show the first variable greater equal than the second variable.

Sample Program :  
#include<iostream>
using namespace std;
int main()
{
  int x = 56, y = 117, c ;
  if( x > y )
  {
    cout <<"x is greater than y\n";
  }
  else
  {
    cout <<" x is not greater than y\n";
  }
  return 0;
}

Output :
x is not greater than y

Related Program :

C Program to find Greatest of Two Numbers


Logical Operators :

We used it to perform some logical operations.

Some Logical Operators and their usage :

&& :

If both of the operands are non zero, then it makes the condition true. It's called Logical AND operator.

|| :

If one of the operands are non zero then it makes the condition true. It's called Logical OR operator.

! :

If the condition is true then it will make the condition false. Basically it's use is to reverse the logical state of the operand. It's called Logical NOT operators.

Sample Program :  
#include<iostream>
using namespace std;
int main()
{
	int x=47 , y = 91 ;
    if (x < y && x !=0)
    {
    	cout <<" All the condition satisfied\n";
    }
    else
    {
    	cout <<"Condition Unsatisfied !!";
    }
    return 0;
}

Output :
All the condition satisfied
 
Related Program :

Vowel or Consonant check in C


Bitwise Operators :

It is used for binary (bit level) calculations.

Some Bitwise Operators and their usage :

& :

It's called Binary AND and is used to copy a bit to the result if it exists in both operands.

| :

It's called Binary OR and is used to copy a bit if it exists in any one operand.

^ :

It's called Binary XOR and is used to copy the bit if it is set in any one operand, not in both.

~ :  

This Binary One's Complement is unary. It has the effect of flipping bits.

Ternary Operators :

We used it as a short hand way of writing conditional statements.

Syntax :

?: :

It's used to present single line conditional expression .

Sample Program :  
#include<iostream>
using namespace std;
int main()
{
	int anik = 56 , deb = 47,  arijit ;
    arijit = (anik < deb) ? anik : deb;
    cout <<"the answer is " <<arijit;
    return 0;
}

//if the condition is true then it will print the first value.
//if the condition is false then it will print the second value.

Output :
the answer is 47

//the value of deb is not bigger than the value of anik. 
//So, the condition is false and as a result the value of deb is printed.

Assignment Operators : 

We used it to assign the value to a variable.
 

Some Assignment Operators and their usage :

= : 

It's used to assigns values from right to left side operands.  

+= :

It's used to add between right and left operands and assigns the answer to the left
operands.
Example : A+=B is same as A= A+B.

-= :

It's used for subtraction between two operands and assigns the answer to the left operands.
Example : A-=B is same as A= A-B. 

*= :

It's used for multiplication between right and left operands and assigns the answer to the 
left operands.
Example : A*=B is same as A= A*B. 

/= :

It's used for division between right and left operands and assigns the answer to the left
operands.
Example : A/=B is same as A= A/B. 

%= :

It's used for modulus division between two operands and assigns the answer respect to left
operands.
Example : A%=B is same as A= A%B.

<<= :

It's called left shift AND assignment operators.
Example : A<<=B is same as A= A<<B.

>>= :

It's called right shift AND assignment operators.
Example : A>>=B is same as A= A>>B.
Sample Program :  
#include<iostream>
using namespace std;
int main()
{
	int x = 18;
    cout <<"value of x is : \n" << x <<endl;
    x += 18;
    cout <<"after assign the value of x is : " <<x;
    return 0;
}

Output :
value of x is : 18 
after assign the value of x is : 36

Misc Operators :

It's the special type operators .

Some Misc Operators and their usage :

sizeof() :

It's used to return the size of a variable.

* :

It's used to pointer a variable.

& :

It's used to scan the address of a variable.

Sample Program :  

#include<iostream>
using namespace std;
int main()
{
    int x;
    cout <<"Enter the value of x : ";
    cin >> x; 
    cout <<"x = " <<x;
    return 0;
}

Output :
Enter the value of x : 56
x = 56

What is precedence of operators ?

The precedence of operators decides that the operators evolution one by one. The movement of the operators may be from left to right or may also from right to left and it's totally depends upon the the associativity. 

Example : 
a = 15 + 8* 2; here, a is assigned 31, not 46 because operator * has higher precedence
than +, so it first gets multiplied with 8*2 and then adds into 15.