operators · arithmetic operator operator meaning example output + addition 4+6 10-subtraction 12-2...

21
OPERATORS Dr. R. Umagandhi

Upload: others

Post on 21-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

OPERATORS

Dr. R. Umagandhi

Page 2: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

OPERATORS

OPERATORS IT IS A SYMBOL USED TO PERFORM AN OPERATION.

EXAMPLE : A + B ; A , B are operands

+ is operator.

TYPES OF OPERATORS

1. ARITHMETIC 5. ASSIGNMENT

2. RELATIONAL 6. CONDITIONAL

3. LOGICAL 7. BITWISE

4. INCREMENT & DECREMENT 8. SPECIAL

Page 3: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Arithmetic Operator

Operator Meaning Example Output

+ Addition 4+6 10

- Subtraction 12-2 10

* Multiplication 5*2 10

/ Division 20/10 2

% Modulo 10%3 1

• The modulo operator gives the remainder only for integers.

• In modulo operator the sign of the result is always depend on the sign

of the dividend.

-5 % 2 = -1

• If the dividend lesser than the deviser then the remainder is the

dividend.

2 % 5 = 2

Page 4: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Program:

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

int a=5,b=3;

cout<<“\tArithmetic Operation”;

cout<<“Addition : “<<a+b;

cout<<“Subtraction : “<<a-b;

cout<<“Multiplication : “<<a*b;

cout<<“Division : “<<a/b;

cout<<“Modulo : “<<a%b;

getch();

}

Page 5: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Output:

Arithmetic Operation

Addition : 8

Subtraction : 2

Multiplication : 15

Division : 1

Modulo : 2

Page 6: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Relational operator

Operator Meaning Example Output

< Less than 3<5 True

> Greater than 8>9 False

<= Less than equal to 4<=4 True

>= Greater than equal

to

4>=4 True

== Double equal 4==6 False

!= Not equal to 4!=6 True

• Relational expression contains relation operators and product either true or

false.

Page 7: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Program 1:

Greater than(>)

#include<iostream.h>

#include<conio.h>

void main()

{

int a=10,b=5;

if(a>b)

cout<<“True”;

else

cout<<“False”;

getch();

}

Less than(<)

#include<iostream.h>

#include<conio.h>

void main()

{

int a=10,b=5;

if(a<b)

cout<<“True”;

else

cout<<“False”;

getch();

}

Page 8: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Less than equal to(<=)

#include<iostream.h>

#include<conio.h>

void main()

{

int a=3,b=3;

if(a<=b)

cout<<“True”;

else

cout<<“False”;

getch();

}

Greater than(>=)

#include<iostream.h>

#include<conio.h>

void main()

{

int a=6,b=5;

if(a>=b)

cout<<“True”;

else

cout<<“False”;

getch();

}

Program 2:

Page 9: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Double Equal(==)

#include<iostream.h>

#include<conio.h>

void main()

{

int a=7,b=7;

if(a==b)

cout<<“True”;

else

cout<<“False”;

getch();

}

Not equal to(!=)

#include<iostream.h>

#include<conio.h>

void main()

{

int a=8,b=-8;

if(a!=b)

cout<<“True”;

else

cout<<“False”;

getch();

}

Program 3:

Page 10: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Logical OperatorLOGICAL OPERATORS ARE USED TO COMBINED THE RELATIONAL EXPRESSION.

SYNTAX:

(RELATIONAL) LOGICAL (RELATIONAL)

EXPRESSION1 OPERATOR EXPRESSION2

Operators Meaning Example Output

&& Logical AND 4<7&&4<6 True

|| Logical OR 4<7||4<3 False

! NOT !(4<7) False

Expression 1 Expression 2 A&&B A||B

T T T T

T F F T

F T F T

F F F F

Page 11: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Logical AND

#include<iostream.h>

#include<conio.h>

void main()

{

if(7<4 && 4<6)

cout<<“True”;

else

cout<<“False”;

getch();

}

Logical OR

#include<iostream.h>

#include<conio.h>

void main()

{

if(4<5||5==6)

cout<<“True”;

else

cout<<“False”;

getch();

}

Program :

NOT

#include<iostream.h>

#include<conio.h>

void main()

{

if(!(4<4)||8==6)

cout<<“True”;

else

cout<<“False”;

getch();

}

Page 12: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Increment and decrement operator

INCREMENT OPERATOR :

++ is an increment operator which increases the value of an operand by 1.

SYNTAX :

++

Variable++;

EXAMPLE :

int a = 10;

a++;

OUTPUT : 11

There are two types of increment,

1 . Pre Increment

2 . Post Increment

Page 13: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

1 . Pre Increment :

The increment operators appears before the operand.

SYNTAX :

++Variable;

EXAMPLE :

++a;

In Pre increment , the right side expression is evaluated and then the

result will be assigned to left side variable.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

int a=5,b;

b=++a;

cout<<a;

cout<<b;

getch();

}

Page 14: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

2 . Post Increment :

The increment operators appears after the operand.

SYNTAX :

Variable++;

EXAMPLE :

a++;

In Post increment , the right side variable assigned to left side

variable and then the right side is evaluated.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

int a=5,b;

b=a++;

cout<<a;

cout<<b;

getch();

}

Page 15: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

DECREMENT OPERATOR :

-- is an decrement operator which increases the value of an operand by 1.

SYNTAX :

--

Variable--;

EXAMPLE :

int a = 10;

a--;

OUTPUT : 9

There are two types of decrement,

1 . Pre Decrement

2 . Post Decrement

Page 16: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

1 . Pre Decrement :

The decrement operators appears before the operand.

SYNTAX :

--Variable;

EXAMPLE :

--a;

In Pre decrement , the right side expression is evaluated and then the

result will be assigned to left side variable.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

int a=7,b;

b=--a;

cout<<a;

cout<<b;

getch();

}

Page 17: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

2 . Post Decrement :

The decrement operators appears after the operand.

SYNTAX :

Variable--;

EXAMPLE :

a--;

In Post decrement , the right side variable assigned to left side

variable and then the right side is evaluated.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

int a=7,b;

b=a--;

cout<<a;

cout<<b;

getch();

}

Page 18: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Assignment Operators

It is used to assign the value of an expression to

variable

Syntax Example : a=4*3

variable = expression; a=12

Update Assignment or Short hand assignmentOPERATOR MEANING EXAMPLE RESULT (if a=10)

+= Addition Assignment a+=4 a=14 (i.e. a=a+4)

-= Subtraction Assignment a-=6 a=4 (i.e. a=a-6)

*= Multiplication

Assignment

a*=2 a=20 (i.e. a=a*2)

/= Division Assignment a/=5 a=2 (i.e. a=a/5)

%= Modulus Assignment a%=3 a=1 (i.e. a=a%3)

Page 19: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

//Assignment Operators

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int a=10

a+=5;

cout<<“\nAddition Assignment=“<<a;

a-=6;

cout<<“\nSubtraction Assignment=“<<a;

a*=2;

cout<<“\nMultiplication Assignment=“<<a;

a/=3;

cout<<“\nDivision Assignment=“<<a;

a%=4;

cout<<“\nModulus Assignment=“<<a;

getch();

return(0);

}

OutputAddition Assignment=15

Subtraction Assignment=9

Multiplication Assignment=18

Division Assignment=6

Modulus Assignment=12

Page 20: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Conditional Operator

?: is used as conditional operator . It is also called as Ternary operator .

Syntax : (expression) ? True : False ;

Example : int a =12,b=10,c;

c=(a<b)?a:b;

cout<<“Value is “<<c

(a<b)?cout<<“,a is lesser”:cout<<“,b is lesser”

O/P – Value is 10,b is lesser

Page 21: OPERATORS · Arithmetic Operator Operator Meaning Example Output + Addition 4+6 10-Subtraction 12-2 10* Multiplication 5*2 10 / Division 20/10 2 % Modulo 10%3 1 •The modulo operator

Bitwise AND

Bitwise Operators

Process

00001000

00001111-------------------------------------------

00001000

Output

A & B = 8

i.e.

000001000=8

Input

A=8

B=15

i.e. 8=00001000

15=00001111

1. Bitwise AND - &

2. Bitwise OR - |

3. Bitwise XOR - ^

4. Left Shift - <<

5. Right Shift - >>