Operators of c++
Operator
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
⇒Relational Operators
⇒Logical Operators
⇒Assignment Operators
⇒Bitwise Operators
⇒Increment/Decrement Operators
⇒Conditional Operators
⇒Special Operator
Arithmetic Operators
Symbol | Operation | Example |
+ | Addition | x+y |
- | Subtraction | x-y |
* | Multiplication | x*y |
/ | Division | x/y |
% | Modulus | x%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
Symbol | Operation | Example |
== | Equal to | 2==3 returns 0 |
!= | Not equal to | 2!=3 returns 1 |
> | Greater than | 2>3 returns 0 |
< | Less than | 2<3 returns 1 |
>= | Greater than or equal to | 2>=3 returns 0 |
<= | Less than or equal to | 2<=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
Symbol | Example | Same as |
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
%= | x%=y | x=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
Symbol | Operation | Example |
& | Bitwise AND | x&y |
| | Bitwise OR | x|y |
<< | Shift Left | x<<2 |
>> | Shift Right | x>>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 */
Increment/Decrement Operators
Symbol | Name | Function | Example |
++ | Increment | It increments the value by 1 | ++x |
-- | Decrement | It 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*/

#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
Symbol | Description | Example |
& | It is used find address of a variable | int 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
Post a Comment
If you have any doubt. Please comment me know.