EEB 3107: Computer Programming: Facilitator: Mr. Kyambille G

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

EEB 3107: Computer Programming

Facilitator: Mr. Kyambille G

EEB 3107_MUST 1
OPERATORS AND OPERANDS

An operator is a high-level computer language specifying an


operation to be performed that yields a value. An operand on the
other hand is an entity on which an operator acts. In an expression
 
A + B

The sign + is an operator specifying the operation of addition on


two operands A and B.

C++ language operators are classified into four types.


Arithmetic operators
Relational operators
Logical/Boolean operators
Assignment operators
EEB 3107_MUST 2
They can be further categorized as:

• Unary operator - Requiring one operand

• Binary operator - Requiring two operands

• Ternary operator - Requiring three operands

Operations associated with these operators are: increment,


decrement and conditional operators.

EEB 3107_MUST 3
Arithmetic Operators [1/3]
Arithmetic operators are those that perform an arithmetic
(numeric) operations.
Unary Operators
Unary arithmetic operators need only one operand for example +a,
–b, namely – (negative) or +(positive).
Binary Operators
Binary arithmetic operators requires two operands and these
include
+ (addition)
- (subtraction or minus)
+ (multiplication)
 / (division)
 % (modulus) - For reminder and it is applied only when
both operands are
 integers.
EEB 3107_MUST 4
Arithmetic Operators [2/3]

Ternary Operators

Ternary arithmetic operators take three operands. For example,


the assignment:
max = a > b ? a : b;
It means that if a > b then max is equal to a otherwise max is
equal to b

The hierarchy of arithmetic operators is such that

* / % are evaluated from left to right


+ - are evaluated from left to right

 
EEB 3107_MUST 5
Arithmetic Operators [3/3]

If parentheses are involved then, expression within parentheses


are evaluated first following the hierarchy given above. In the
event of nested parentheses, the C++ compiler will evaluate the
innermost parentheses first, then moving outwards and finally the
hierarchy given is followed

Example 3.1
1 + ((3 + 1) / (8 – 4) – 5)
Solution
1 + ((3 + 1) / (8 – 4) – 5)  1 + (4 / (8 – 4) – 5)
 1 + (4 / 4 – 5)
 1 + (1 – 5)
 1 + -4
 -3

EEB 3107_MUST 6
Relational Operators

Relational operators are used to test the relation between two

values. All C++ relational operators are binary operators and hence

require two operands. A relation expression is made up of two

arithmetic expressions connected by a relation operator. It

returns a zero when the relation is FALSE and a non-zero when a

relation is TRUE. The table below summarizes relational operators.

EEB 3107_MUST 7
Table: Relational operators

Operator Symbol Form Result

Grater than >  a>b 1 if a is grater than b, else 0

Less than <  a<b 1 if a is less than b, else 0

Greater than or equal to >= a >= b 1 if a is grater than or equal to b, else 0

Less than or equal to <= a <= b 1 if a is less than or equal to b, else 0

Equal to == a==b 1 if a is equal to b, else 0

Not equal to != a != b 1 if a is not equal to b, else 0

EEB 3107_MUST 8
Note that all relational operators have lower precedence than the
arithmetic operators.

EEB 3107_MUST 9
Example

Given the following declarations:

int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;

Evaluate the following expression

m/n<x
 

Solution
m/n<x  (m / n) < x
 -1 < 2.5
 T = 1
EEB 3107_MUST 10
Exercise 1

Given the declarations in example above, evaluate the following


expressions:

Expression
j>m
j <= m >= n
j <= x == m
-x + j == y > n >= m
x += (y >= n)
++j == m != y * 2

EEB 3107_MUST 11
Logical or Boolean Operators

A logical operator combines the result of one or more expressions


and the resultant expression is called the logical expression. After
testing the condition, they return logical status TRUE or FALSE
as net result.

Logical operators are unary or binary operators. The operand may


be constants, variables or expressions. Table below gives logical
operators.

EEB 3107_MUST 12
Table: Boolean Operators

Operator Symbol Form Results

Logical AND && a && b 1 if a and b are non zero else


0

Logical OR ¦¦ a¦¦ b 1 if a or b is non zero else 0

Logical ! !a 1 if a is 0; else 0
negation

EEB 3107_MUST 13
In algebra, the expression

x<y<z

it is true if y is greater x or less than z. In c++ however, the


expression has a different meaning since it is evaluated from left
to right as follows.

(x < y) < z

The result of x < y is either 0 or 1. The expression is therefore


true in c++ is x is less than y and z is greater 1, or if x not less
than y and z is greater than zero.

EEB 3107_MUST 14
Hierarchy of Logical Operators

The logical NOT has higher precedence than the others. The AND

operator (&&) has higher precedence than OR operator ( || ). Both

the logical AND and logical OR operators have lower precedence

than the relational and arithmetic operators.

EEB 3107_MUST 15
Exercises 2

Given the following declarations


int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;
Evaluate the following expressions.
j && m
j < m && n < m
m + n || !j
x * 5 && 5 || m/n
j <= 10 && x >= 1 && m
!x || !n || m + n
x * y < j + m ||n
(x > y)| !j || n++
(j || m) + (x || ++n)

EEB 3107_MUST 16
Conditional Operator (?)

The conditional Operator: takes three operands. The value of an


expression using the conditional operator is the value of either the
second or the third operand, depending on the value of the first
operand. For example consider the following expression:
z = ((x < y) ? x : y);
This expression means that:

if (x < y)
z = x;
else
z = y;

Note that in the expression above, the first operand is the test
condition. The second and third operand represents the final value
of the expression. The condition operator can be summarized as
EEB 3107_MU
shown in table. 17
Conditional Operator (?)

Table: Conditional Operators

Operator Symbol Form Results

Conditional ?: a?b:c 1 if a is non result is b:


otherwise result is C

EEB 3107_MUST 18
Precedence of Operators

All operators have two important properties called precedence and

associatively. Operators with higher precedence have their

operands bound, or grouped together before operators of lower

precedence regardless the order in which they appear. Table below

shows the precedence of deferent operators and their associatively.

EEB 3107_MUST 19
Table 7: Precedence of Operators…………..

Class of Operator Operation Associativity Precedence

Unary -+   Highest
Multiplicative * / % Left-to-right ^

Additive + - Left-to-right |

Relational < <= > >= Left-to-right |

Equality = = != Left-to-right |

Logical END && Left-to-right |

Logical OR ¦¦ Left-to-right |

conditional ?: right-to-left |
Assignment = += -= *= /= %= Right-to-left |

Comma , Left-to-right Lowest

EEB 3107_MUST 20
Precedence of Operators

Example

Given the following declarations


int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;

Evaluate the following expressions


j <= x == m
Solution:

j <= x == m ((j <= x) == m)


((0 <= 2.5) == 1)
T == 1

EEB 3107_MUST 21
Precedence of Operators

Exercise

Given declarations in example above, evaluate the following


expressions:

Expression Possible
Solutions
a) j>m 0

b) m/n<x 1
c) j <= m >= n 1
d) -x + j = = y > n >= m 0
e) x += (y >= n) 3.5
f) ++j = = m != y * 2 1
EEB 3107_MUST 22
Thank you for Listening!

EEB 3107_MUST 23

You might also like