operators · arithmetic operator operator meaning example output + addition 4+6 10-subtraction 12-2...
TRANSCRIPT
OPERATORS
Dr. R. Umagandhi
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
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
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();
}
Output:
Arithmetic Operation
Addition : 8
Subtraction : 2
Multiplication : 15
Division : 1
Modulo : 2
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.
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();
}
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:
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:
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
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();
}
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
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();
}
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();
}
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
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();
}
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();
}
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)
//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
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
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 - >>