0% found this document useful (0 votes)
5 views

Lecture 3 Operator

The document provides an overview of different types of operators in programming, including unary, binary, and ternary operators, along with their functions and examples. It explains assignment, arithmetic, logical, relational, and conditional operators, detailing how they operate on operands and the rules for expression evaluation. Additionally, it includes exercises for practice to reinforce understanding of operator types and expression evaluation.

Uploaded by

islamrafiul046
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 3 Operator

The document provides an overview of different types of operators in programming, including unary, binary, and ternary operators, along with their functions and examples. It explains assignment, arithmetic, logical, relational, and conditional operators, detailing how they operate on operands and the rules for expression evaluation. Additionally, it includes exercises for practice to reinforce understanding of operator types and expression evaluation.

Uploaded by

islamrafiul046
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Operator

Operator
vOperator: An operator is a symbol that tells the compiler to
perform certain mathematical, logical or relational manipulation.

Operators

Unary Operator Binary Operator Ternary Operator


-, ++, -- ?:

Assignment Arithmetic Logical Relational


Operator Operator Operator Operator
= +, -, *, /, % &, |, !, <<, >> <, >, <>, !=, ==
&&, ||, >=, <=
Unary Operator
v Unary Operator: A unary operator acts upon a single operand to
produce a new value.
Key unary operators are – (negation), ++ (increment), -- (decrement)
– (negation): examples -5, -3, -a, etc.
++ (increment): ++a or a++ is equivalent to a = a + 1, i.e., the value of a will be
incremented by 1. However, there is a difference between ++a and a++ when
associated with other operators. For example,
++a * b : ++a will be implemented before * operation.
a++ * b : * operation will be implemented before ++a operation.
-- (decrement): --a or a-- is equivalent to a = a - 1, i.e., the value of a will be
decremented by 1. However, there is a difference between --a and a-- when
associated with other operators. For example,
--a * b : --a will be implemented before * operation.
a-- * b : * operation will be implemented before --a operation.
Binary Operator
vBinary Operator: A binary operator acts upon two
operands to produce a new value.

Binary Operator

Assignment Arithmetic Logical Relational


Operator Operator Operator Operator
= +, -, *, /, % &, |, !, <<, >> <, >, <>, !=, ==
&&, ||, >=, <=
Assignment Operator
= is called the assignment operator. This is not the equal
operator. Only one variable can be placed left side of =, but in
the right side, an expression or condition may be placed.
Expression: Some examples of expression are:
a, 2, a+2, (a+b)/2*c
Condition = exp Rop exp
exp = expression Rop = Relational Operator
Some examples of condition,
a == 2, a < b, a+2 && (a+b)/2*c
The output of condition is always either 1 (True) or 0 (False).
Equation: variable = expression or condition
a=2 a=(a+b)/2*c e = a + 2 && (a+b)/2*c
Arithmetic Operator
Binary arithmetic operators are + (addition), -(minus), *
(multiplication), / (division) and % (modulus or mod in brief).

% operator: this operator produces the remainder of division as the new


value. For example, 7 % 2 = 1, 34 % 10 = 4, 7 % 10 = 7

Type Auto Conversion: During binary operation, if the types of two


operands are different, then auto type conversion takes place in the following
way: char -> int int -> float char -> float
Example: float S, A;
int B; S = A + B;
In this example, A is float and B is int. For performing + operation, the types of
two operands shall be the same. As float type cannot be converted to int, int
type will be converted to float. So, the value stored in B will be converted to float
and then the result will be float type.
Arithmetic Operator
Type Casting: Consider the example,
int S, A;
float B; S = A + B;
ü For performing addition operator, the value of A will be converted to float
type and the result will be float type.
ü As float type value cannot be stored in integer location, an error will be
occurred with the execution of the statement, S = A + B;
The solution of this problem is type casting. The correct statement is given
below:
S = (int) ( A + B); or
S = A + (int) B;
Both of the above statements are correct and none of them will not generate
any error.
Arithmetic Operator
Exercise: int i, j; long ix;
short s; float x;
double dx; char c;
Determine the type of following expressions: (a) i + c (b) x + c (c) dx + x
(d) ((int) dx) + ix (e) i + x (f) s+j (g) ix + j (h) s + c (i) ix + c

Solution:
(a) i + c (b) x + c (c) dx + x (d) ((int) dx) + ix
= int + char = float + char = double + float = ((int) double) + long
= int = float = double = int + long = long

(e) i + x (f) s+j (g) ix + j (h) s + c (i) ix + c


= int + float = short + int = long + int = short + char = long + char
= float = int = long = short = long
Logical Operator
ü Logical operator is also known as bitwise operator.
ü For logical operation, data must be represented into bits.

A B & A B |
0 0 0 0 0 0 A !
0 1 0 0 1 1 0 1
1 0 0 1 0 1 1 0
1 1 1 1 1 1

Example: if A = 13 and B = 18, find (a) A & B; (b) A | B


A = 0000 0000 0000 1101 A = 0000 0000 0000 1101
B = 0000 0000 0001 0010 B = 0000 0000 0001 0010
A & B = 0000 0000 0000 0000 A | B = 0000 0000 0001 1111
=0 = 31
<< is the left shift operation. For example, A << 2 will produce the output
0000 0000 0011 0100. Value is multiplied by 22= 4.
>> is the right shift operation. For example, A >> 2 will produce the output
0000 0000 0000 0011. Value is divided by 22= 4 (integer division).
Relational Operator
q Relational operator always acts like a question. For example,
a<b whether a is less than b or not?
Answer is always either 1 (True) or 0 (False).
q Relational operator always makes a condition.
Exercise: int i = 8, j = 5;
float x = 0.005, y = -0.01;
Determine the value of following expressions: (a) ! ( i <= j)
(b) (x > y) && ( i > 0) || (j < 5)
Solution: (b) (x > y) && ( i > 0) || (j < 5)
a) ! ( i <= j) = (0.005 > -0.01) && (8 > 0) || (5 < 5)
= ! ( 8 <= 5) = 1 && 1 || 0
= ! (0) = 1 || 0
= 1 =1
Ternary Operator
q Example of ternary operation is, if (a < b)
x = a < b ? a: b; x = a;
equivalent to
else
x = b;
Ternary operator is also known as conditional operator.
Exercise: What is the value assigned to x if b is 7?
x = b > 8 ? b << 3 : b > 4 ? b >> 1 : b;
Solution:
(b) x = b > 8 ? b << 3: b > 4 ? b >> 1 : b;
= b > 4 ? b >> 1: b [ since, b > 8 is false]
= b >> 1 [ since b > 4 is true]
= 7 / 21
=7/2
=3
Expression Evaluation
v Precedence Rule: Which operation will be done first?
v Associativity Rule: Whether the operation will be executed from
left to right or right to left?

Operator Category Operators Associativity


Unary operators -, ++, --, !, sizeof R->L
Arithmetic multiplication and division *, /, % L -> R
Arithmetic add and subtract +, - L -> R
Relational Operators <, >, <>, <=, >=, &&, ||, == L -> R
Conditional Operators ?: R -> L
Assignment Operators =, +=, -=, *=, /=, %= R -> L
Expression Evaluation
Exercise: int i = 8, j = 5;
float x = 0.005, y = -0.01;
char c = ‘c’, d = ‘d’;
Determine the value of following expressions:
(a) ( i – 3 * j) % ( c + 2 * d) / ( x – y)
(b) ( 3 * i – 2 * j ) % ( 2 * d – c ) + ( j != 6)

Solution:
(a) ( i – 3 * j ) % ( c + 2 * d) / (x – y) (a) ( 3*i – 2*j ) % (2*d-c)+ (j != 6)
= (8–3*5) % (99+2 *100)/(0.005 –(-0.01)) = (3*8 – 2*5)%(2*100-99)+(5 !=6)
= (8 – 15) % (99 + 200)/(0.005 +0.01) = (24-10) %(200-99)+ 1
= -7 % 299/0.015 = 14 % 101 + 1
= -7/0.015 = 14 + 1
= -466.67 = 15
For Your Practice
Exercise: int i = 8, j = 5;
float x = 0.005, y = -0.01;
char c = ‘c’, d = ‘d’;

1. Determine the type and the value of following expressions:


(a) 2 * ( ( i / 5 ) + ( j – 3 ) % ( i + j – 2 )
(b) ( x > y ) && ( i > 0 ) && ( j < 5 )
2. What is the value of i?
(c) i -= ( j > 0 ) ? j : 0;
(d) i %= j;

You might also like