Operators

Operator is a symbol used to perform the operation on the opponents.
Operators are classified into two types:
1.Unary Operator
2.Binary Operator
An operator which is used to perform the operation on a single opponent is known as 'Unary Operator'.
An operator which is used to perform the operation on more than one opponent ( minimum of two ) is known as 'Binary Operator'.

Type of operators:
1.Assignment operator
2.Arithmetic operator
3.Relational operator
4.Logical operator
5.Conditional operator
6.Shortcut operator / Compound operator
7.Size of operator
8.Increment and decrement operator

1.Assignment operator:

Assignment operator is used to assign a value or expression.
Assignment always takes place from right to left.
Assignment operator is " = " ( equal )
Syntax: < data type > < variable name > [ = value ]
Example:
int x; //declaration
x=10; //Assigning value
int x = 10; //variable declaration and initialization
x = x + 10; // now x= 10 + 10 = 20 --> x = 20
 Multi-assignment :
Example: x1 = x2 = x3 = 10

2.Arithmetic operator:

Used to perform arithmetic operations i.e., mathematical operations like addition, subtraction, multiplication, division, modules.
Let x = 10, y= 5

 Operation Expression Action   Result
     +     x + y 10 + 5   15 
      -   x - y 10 - 5   5
     x   x * y 10 * 5   50
     /   x / y 10 / 5   2
     %  x % y 10%5   0


Example : d = ( 2 + 3 ) * (3 + 2) //Output:25
(BOD MAS is followed)

3.Relational operator:

Relational operators are used to check the relation among the operators and it will return boolean value ( 0 or 1, 0 for false and 1 for true ).
Let x = 10 and y = 20
Operator  Expression   Action   Result
      >              x>y            10>20       0
    <              x<y            10<20       1
    >=            x>=y         10>=20      0
    <=            x<=y         10<=20      1
    ==            x==y         10==20      0
    !=             x!=y          10!=20       1

4.Logical operator:

Logical operators are used when ever we want to use more than one condition.
Logical operators are AND, OR, NOT.
i.AND(&&):
Represented by '&&' symbol.
Truth table:
x  y    x&&y
0  0     0
0  1     0
1  0     0
1  1     1
Example: Let x=10, y=20, z=30
  Operation         Result
(x>y)&&(x>z)        0
(y>x)&&(x>z)        0
(z>x)&&(z>y)        1
ii.OR(||):
Represented by '||' symbol.
Truth table:
x  y    x||y
0  0     0
0  1     1
1  0     1
1  1     1
Example: Let x=10, y=20, z=30

  Operation         Result
(x>y)&&(x>z)        0
(y>x)&&(x>z)        1
(z>x)&&(z>y)        1
iii.NOT(!):
Represented by '!' symbol.
Truth table:
x        !x
0       1
1       0
Example: Let x=10, y=20
  Operation         Result
     !(x>y)                1

5.Conditional operator:

The conditional operator will check the condition initially, if the condition is true then it will execute logical true statement otherwise logical false statement.
Conditional operator is also called as 'Ternary operator'.
Denoted by ? :
Syntax: (<Condition>)?Logical_true : Logical_false;
Example: Let x=10, y=20
(x>y)?printf("x is big"):("y is big");
Output: y is big

6.Shortcut operator:

This is also called as 'compound operator'.
It is a combination of arithmetic and assignment operators.

 Operators  Action      Meaning
     += 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

7.Size of operator:

With the help of sizeof() we can find the number of bytes taken by a variable or a data type or a function.
Note: sizeof(sizeof()) gives an error
Example: sizeof(int) //output:2

8.Increment and Decrement operator:

Increment operator is '++' used to increase the value.
Decrement operator is '--' used to decrease the value.
Example: Let x=10
x=x+1; or x++; //gives 11
x=x-1; or x--; //gives 9 as output
Increment and decrement operators are classified into following types
i.Pre-Increment operator(++x):
Frist increments the value and then prints the result

ii.Post-Increment operator(x++):
First prints the previous value and then increments the value

iii.Pre-Decrement operator(--x):
First decrements the value and then prints the result

iv.Post-Decrement operator(x--):
First prints the previous value and then decrements the value


Post a Comment

0 Comments