Back

Operators

OPERATORS

We use various operators for various operations on values that we use as data. These values need to be added mathematically or compared based on relations among them. Depending on the need, the operator is used to get desired result.

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.

OperatorFunctionalityIf x=5; y=10;
>More Thanx>y; False
<Less Thanx<y; True
>=More Than or Equal Tox>=y; False
<=Less Than or Equal Tox<=y; True
==Equal tox==y; False
!=Not Equal tox!=y; True

When we need to combine two or more situations of conditional checking we use, conditional operators.

 

OperatorFunctionalityIf 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 resultx!<y                 False

 

We also have commonly used arithmetic operators. These are used to compute arithmetic value as per user requirement.

OperatorFunctionalityIf x=5; y=10;
+Additionx+y   =                15
Subtractionx-y    =               -5
*Multiplicationx*y    =               50
/Divisionx/y    =               0
%Remainderx%y   =              5

The operators that work with a single operand are called Unary operator

 

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

OperatorFunctionalityEquivalent 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;

Subscribe to our newsletter for courses and tutorial updates.