Whenever we need to make a comparison while checking a condition, we use relational operators. Let’s consider x=5; y=10; for the following operators’ functionality.
Operator | Functionality | If x=5; y=10; |
> | More Than | x>y; False |
< | Less Than | x<y; True |
>= | More Than or Equal To | x>=y; False |
<= | Less Than or Equal To | x<=y; True |
== | Equal to | x==y; False |
!= | Not Equal to | x!=y; True |
When we need to combine two or more situations of conditional checking we use, conditional operators.
Operator | Functionality | If x=5; y=10; |
&& | Both the sides of operator should result true | x<y && y!=x True x<y && y==x False |
|| | Either side of operator should result true | x<y || y!=x True x<y || y!=x True |
! | Inverse the result | x!<y False |
We also have commonly used arithmetic operators. These are used to compute arithmetic value as per user requirement.
Operator | Functionality | If x=5; y=10; |
+ | Addition | x+y = 15 |
– | Subtraction | x-y = -5 |
* | Multiplication | x*y = 50 |
/ | Division | x/y = 0 |
% | Remainder | x%y = 5 |
The operators that work with a single operand are called Unary operator
Operator | Functionality | Equivalent to |
++ (Post) | Increment x++; | x=x+1; |
–(Post) | Decrement x–; | x=x-1; |
++ (Pre) | Increment ++x; | x=x+1; |
–(Pre) | Decrement –x; | x=x-1; |
! | Inverse the result r=x<y; r=!r; | False |
There are also certain shorthand operators known as compound assignment operator.
Operator | Functionality | Equivalent to |
+= | x=5; x+=5; | x=x+5; |
-= | x=5; x-=5; | x=x-5; |
*= | x=5; x*=5; | x=x*5; |
/= | x=5; x/=5; | x=x/5; |
%= | x=5; x%=5; | x=x%5; |