Operators of c++


Operator

It is a special symbol which is used to perform logical or mathematical operation on data or variable.

Operand
It is a data or variable on which the operation is to be performed.

Types of Operator

⇒Arithmetic Operators
⇒Relational Operators
⇒Logical Operators
⇒Assignment Operators
⇒Bitwise Operators
⇒Increment/Decrement Operators
⇒Conditional Operators
⇒Special Operator

Arithmetic Operators

SymbolOperationExample
+Additionx+y
-Subtractionx-y
*Multiplicationx*y
/Divisionx/y
%Modulusx%y
#include<iostream>
using namespace std;
int main()
{
 int a=5,b=3;
 cout<<(a+b)<<"\n";
 cout<<(a-b)<<"\n";
 cout<<(a*b)<<"\n";
 cout<<(a/b)<<"\n";
 cout<<(a%b)<<"\n";//%(modulus) holds remainder
}
/*
### Output ###
8
2
15
1
2

*/

Relational operator


SymbolOperationExample
==Equal to2==3 returns 0
!=Not equal to2!=3 returns 1
>Greater than2>3 returns 0
<Less than2<3 returns 1
>=Greater than or equal to2>=3 returns 0
<=Less than or equal to2<=3 returns 1

Logical Operators


(x>y)&&(x>z)
Here this expression returns true if both conditions are true.
(x>y)||(x>z)
Here this expression returns true if any one or both conditions are true.
!(x>y)
Not operator reverses the state means if the condition is true it returns false and if the condition is false it returns true.

#include<iostream>
using namespace std;
int main()
{
int a=10,b=50,c=30;
if(a>b&&a>c)
cout<<"a is greatest";
if(b>a&&b>c)
cout<<"b is greatest";
if(c>a&&c>b)
cout<<"c is greatest";
}
/*
### Output ###
b is greatest

*/


 
Assignment operator
SymbolExampleSame as
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
%=x%=yx=x%y
#include<iostream>
using namespace std;
int main()
{
int x1=5,y1=3;
x1+=y1;//x1=x1+y1
cout<<"x1="<<x1<<"\n";

int x2=5,y2=3;
x2-=y2;//x2=x2-y2
cout<<"x2="<<x2<<"\n";

int x3=5,y3=3;
x3*=y3;//x3=x3*y3
cout<<"x3="<<x3<<"\n";

int x4=5,y4=3;
x4/=y4;//x4=x4/y4
cout<<"x4="<<x4<<"\n";

int x5=5,y5=3;
x5%=y5;//x5=x5%y5
cout<<"x5="<<x5<<"\n";
}
/*
### Output ###
x1=8
x2=2
x3=15
x4=1
x5=2

*/


Bitwiseopretor
SymbolOperationExample
&Bitwise ANDx&y
|Bitwise ORx|y
<<Shift Leftx<<2
>>Shift Rightx>>2
#include<iostream>
using namespace std;
int main()
{
 int a=5,b=3,c;//variable declaration
 c=a&b;        // AND operation
 cout<<"a&b="<<c<<"\n";
 c=a|b;        // OR operation
 cout<<"a|b="<<c<<"\n";
 c=a>>2;        // shift right operation
 cout<<"a>>2="<<c<<"\n";
 c=a<<2;        // shift left operation
 cout<<"a<<2="<<c<<"\n";
}
/*
### output ###
a&b=1
a|b=7
a>>2=1
a<<2=20
*/
Fore more details click here

Increment/Decrement Operators

SymbolNameFunctionExample
++IncrementIt increments the value by 1++x
--DecrementIt decrements the value by 1--x
#include<iostream>
using namespace std;
int main()
{
 int a=5,b=10;
 cout<<++a<<endl;
 cout<<--b;
}
/*
### output ###
6
9

*/

Conditional Operators




#include<iostream>
using namespace std;
int main()
{
 int a=10,b=20;
a>b?cout<<"a is greater than b":cout<<"b is greater than a";
}
/*
### Output ###
b is greater than a
*/

 Special Operators
SymbolDescriptionExample
&It is used find address of a variableint a=10;
cout<<&a;
*It is used to declare pointer type variable.int *p;
sizeof()It returns the memory size of variable.int a=10;
cout<<sizeof(a);

Comments

Popular posts from this blog

Function in C++ with example & declaration, initialization function in c++

Storage class in c++