Operators
Operators are the symbol that tells the computer to perform certain mathematical and logical
operation. And the data item or value on which operator perform the certain operation is called
operand.
Classification
- Arithmetic operator
- Relational operator
- Logical operator
- Assignment operator
- Conditional operator
- Bitwise operator
1. Arithmatic operator
The arithmetic operator perform arithmatic operation. It can be classified into Unary & Binary
operator.
a) Unary operator
The operator which required single operand for operation is known as unary operator.
Example:
Unary Operator |
Purpose |
a++ |
(increment operator) final value a+1 |
a-- |
(decrement operator) final value a-1 |
-p |
(Unary minus) positive p change to -ve i.e. -p |
b) Binary operator
The operator which required two operand for operation is known as binary operator.
Example:
Binary operator |
Purpose |
+ |
Addition (a+b) |
- |
Subtraction (a-b) |
* |
Multiplication (a*b) |
/ |
Division (a/b) |
% |
Modulus (a%b) |
2. Relational Operator
The relational operators are the symbols which is used to compared two or more subject matter.
Example:
Relational operator |
Purpose |
<< /td>
| (smaller than) a < 5 |
> |
(greater than) a > 5 |
<=< /td>
| (smaller than equal to) a <= 5 |
>= |
(greater than equal to) a >= 5 |
== |
(equal to) marks == 32 |
!= |
(not equal to) mark != 32 |
3. Logical Operator
The logical operator are used to combine two or more than two expression of relational operator.
Example:
Logical operator |
Purpose |
&& |
(and) if(age>50 && salary>5000) |
|| |
(or) if(age<50 || age>20) |
! |
(not) mark != 32 |
4. Assignment Operator
Assignment operator are used to assigne a value of right hand variable to left hand variable.
Example:
A = l x b
sum = a + b
Difference between:
Assignment operator (=) |
Equality operator (==) |
Assignment operators is used to assign a value to a variable. |
Equality operator is used to determine if two expression have the same value or not.
|
5. Conditional Operator (Ternary Operator)
The conditional operator consists of two symbols and three expression. The symbols are (?) and colon
(:). The conditional expression is written as
Expression1 ? Expression2 :
Expression3
Here, Expression1 is evaluated first. If it is true, then Expression2 is evaluated else Expression3
is evaluated. This is also called short cut version of if else statement. Example:
a>b ? a-b : a+b
Here, if a is greater than b then Expression2 is evaluated else Expression3 is evaluated.
6. Bitwise Operator
A bitwise operator operates on each bit of data. Usually bitwise operators are not useful in cases
of float and double variable.
Operators |
Symbol |
Working function |
Example |
Bitwise AND |
& |
If both ON then ON else OFF |
100 & 101 = 100 |
Bitwise OR |
| |
If any one ON then ON else OFF |
100 | 101 = 101 |
Bitwise XOR (Exclusive OR) |
^ |
If both ON or OFF then OFF else ON |
100 ^ 101 = 001 |
Left shift |
<<< /td>
| Indicates the bits are to be shifted to the left |
1111<<2=> 1100 |
Right shift |
>> |
Indicates the bits are to be shifted to the right. |
1111>>2 => 0011 |
Precedence & Associativity
Precedence
In a statement consisting of multiple operators, the criteria or rules that determine which operator
should be operated or carried out first is called precedence of an operator.
Consider the following two statement
int a = 1, b = 2, c = 3, d;
d = a + b * c;
Here, b is multiplied by c first. The result is added to a and the sum is assigned to d. Since
multiplication is done before addition the multiplication operator has a highest precedence than the
addition one.
Associativity
If an expression contain two or more than two operator which have same precedence then priority is
evaluated either from left to right or from right to left. This is known as as associativity.
For example in the expression
d = a - b - c ;
The first (left-most) minus is evaluated first. The variable is subtracted from a. Then the second
minus is evaluated, causing c to be subtracted from the result.