Operator:
. An object that is capable of manipulating value or operand.
Types of Operators:
1. Arithmetic Operators:
Operator | Description |
---|---|
+ | Adds two values |
- | Subtracts two values |
* | Multiplies two values |
/ | Divides two values |
% | Returns remainder of two values |
2. Logical / Relational Operators:
Operator | Description |
---|---|
== | Returns true if two operands are equal |
!= | Returns true if two operands are unequal |
> | Returns true if left value is greater than right |
< | Returns true if left value is less than right |
>= | Returns true if left value is greater than or equal to right |
<= | Returns true if left value is less than or equal to right |
&& | Returns true if both expressions are true |
|| | Returns true if either of expressions is true |
! | Reverses the value of expression |
3. Bitwise Operators:
. Works on bits.
. Converts value of operand into bits and perform operations.
Operator | Description |
---|---|
& | Performs Binary AND between two operands |
| | Performs Binary OR between two operands |
^ | Performs Binary XOR between two operands |
~ | Complements value |
<< | Performs Binary shift left |
>> | Performs Binary shift right |
4. Assignment Operators:
Operator | Description |
---|---|
= | Simple Assignment |
+= | Adds and assigns result in left operand |
-= | Subtracts and assigns result in left operand |
*= | Multiplies and assigns result in left operand |
/= | Divides and assigns result in left operand |
%= | Perform modulus and assigns result in left operand |
<<= | Perform binary left shift and assigns result in left operand |
>>= | Perform binary right shift and assigns result in left operand |
&= | Perform logic AND and assigns result in left operand |
|= | Perform logic OR and assigns result in left operand |
5. Unary Operators:
Operator | Description |
---|---|
+ | Unary plus |
- | Unary minus |
++ | Post/Pre Increment |
-- | Post/Pre Decrement |
Precedence:
. Characteristic of operators which indicates when they will be evaluated when they appear in expression.
. Example: '*' has higher precedence than '+'
. When several operations occur in expression, each part is evaluated and resolved in an order.
. Mathematical expressions should follow BODMAS rule which shows precedence of using operator in levels.
. Mathematical expressions should follow BODMAS rule which shows precedence of using operator in levels.
Operator | Precedence |
---|---|
Postfix | expr++ expr-- |
Unary | ++expr --expr +expr -expr |
Multiplicative | * / % |
Additive | + - |
Shift | << >> |
Relational | < > <= >= |
Equality | == != |
Bitwise AND | & |
Bitwise XOR | ^ |
Bitwise OR | | |
Logical AND | && |
Logical OR | || |
Assignment | = += -= *= /= %= <= >= |