Professional Documents
Culture Documents
EEB 3107: Computer Programming: Facilitator: Mr. Kyambille G
EEB 3107: Computer Programming: Facilitator: Mr. Kyambille G
EEB 3107: Computer Programming: Facilitator: Mr. Kyambille G
EEB 3107_MUST 1
OPERATORS AND OPERANDS
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
EEB 3107_MUST 5
Arithmetic Operators [3/3]
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
values. All C++ relational operators are binary operators and hence
EEB 3107_MUST 7
Table: Relational operators
EEB 3107_MUST 8
Note that all relational operators have lower precedence than the
arithmetic operators.
EEB 3107_MUST 9
Example
int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;
m/n<x
Solution
m/n<x (m / n) < x
-1 < 2.5
T = 1
EEB 3107_MUST 10
Exercise 1
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
EEB 3107_MUST 12
Table: Boolean Operators
Logical ! !a 1 if a is 0; else 0
negation
EEB 3107_MUST 13
In algebra, the expression
x<y<z
(x < y) < z
EEB 3107_MUST 14
Hierarchy of Logical Operators
The logical NOT has higher precedence than the others. The AND
EEB 3107_MUST 15
Exercises 2
EEB 3107_MUST 16
Conditional Operator (?)
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 (?)
EEB 3107_MUST 18
Precedence of Operators
EEB 3107_MUST 19
Table 7: Precedence of Operators…………..
Unary -+ Highest
Multiplicative * / % Left-to-right ^
Additive + - Left-to-right |
Equality = = != Left-to-right |
Logical OR ¦¦ Left-to-right |
conditional ?: right-to-left |
Assignment = += -= *= /= %= Right-to-left |
EEB 3107_MUST 20
Precedence of Operators
Example
EEB 3107_MUST 21
Precedence of Operators
Exercise
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