Week2-Operators
Week2-Operators
Computer Programming 2
Operators
1
Operators Overview
Arithmetic Operators
Assume variable A
holds 10 and
variable B holds
20 then:
ITC121-Computer Programming 2 1
Arithmetic (cont…)
Integer Division
Integer division truncates
Floating point division produces floating point
numbers 10 / 2 = 5
9/2=4
99 / 100 = 0
10.0 / 2.0 = 5.0
99.0 / 100.0 = 0.99
5
ITC121-Computer Programming 2 2
Relational Operators
Assume variable A
holds 10 and
variable B holds
20 then:
Relational (cont…)
Assume variable A
holds 10 and
variable B holds
20 then:
Relational (cont…)
▰ Boolean logic is based on true or false comparisons
▰ Boolean variables can hold only one of two values: true or false
▰ You can assign values based on the result of comparisons to Boolean variables
ITC121-Computer Programming 2 3
Logical Operators
Logical operators are used to perform operations based on
simultaneous evaluation of multiple expressions.
▰ && - AND
▰ || - OR
Ex: && - AND
▰ if(a==b && b!=c)
if a is equal to b and b is not equal to c: this condition will evaluate to true
[if and only if (a==b) is true and (b!=c) is true]
Ex: || - OR
▰ if(a==b || b!=c)
if a is equal to b or b is not equal to c - this condition will evaluate to true
[if either of (a==b) is true or (b!=c) is true]
10
10
Logical (cont…)
Assume variable A
holds Boolean
value true and
variable B holds
Boolean value
false, then:
11
11
Bitwise Operators
Assume variable
A holds 60 and
variable B holds
13, then:
12
12
ITC121-Computer Programming 2 4
Bitwise (cont…)
Assume variable
A holds 60 and
variable B holds
13, then:
13
13
Assignment Operators
14
14
Assignment (cont…)
15
15
ITC121-Computer Programming 2 5
Assignment (cont…)
16
16
Assignment (cont…)
▰ Example:
17
17
Operators Precedence
Category Operators
Postfix ()[]
Positive value of: +
Negative value of: -
Not: !
Unary
Bitwise complement: ~
Pre-increment: ++x
Post-decrement: x--
Multiply: *
Multiplicative Divide: /
Division remainder: %
18
18
ITC121-Computer Programming 2 6
Operators Precedence
Category Operators
Add: +
Additive
Subtract: -
Shift bits left: <<
Shift
Shift bits right: >>
Less than: <
Greater than: >
Less than or equal to: <=
Relational
Greater than or equal to: >=
Type equality/compatibility: is
Type conversion: as
19
19
Operators Precedence
Category Operators
Equals: ==
Equality
Not equals: !=
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Logical OR ||
20
20
Operators Precedence
Category Operators
Ternary conditional ?:
21
21
ITC121-Computer Programming 2 7
Operator Precedence Rules
22
22
Precedence
PEMDAS:
Unary: +, - high
Multiplicative: *, /, %
Additive: +, -
Relational operators
Shortcut: +=, -=, *=, /=, %= low
23
23
Precedence (cont…)
x = 1 + 2 *3 / 4 % 5
x=2
Parenthesis
Power
Multiplication
Addition
Left to Right
24
24
ITC121-Computer Programming 2 8
Precedence (cont…)
1+2*3/4%5
x = 1 + 2 *3 / 4 % 5
x=2
Parenthesis
Power
Multiplication
Addition
Left to Right
25
25
Precedence (cont…)
1+2*3/4%5
x = 1 + 2 *3 / 4 % 5
x=2 1+6/4%5
Parenthesis
Power
Multiplication
Addition
Left to Right
26
26
Precedence (cont…)
1+2*3/4%5
x = 1 + 2 *3 / 4 % 5
x=2 1+6/4%5
1+1%5
Parenthesis
Power
Multiplication
Addition
Left to Right
27
27
ITC121-Computer Programming 2 9
Precedence (cont…)
1+2*3/4%5
x = 1 + 2 *3 / 4 % 5
x=2 1+6/4%5
1+1%5
Parenthesis
Power
Multiplication 1+1
Addition
Left to Right
28
28
Precedence (cont…)
1+2*3/4%5
x = 1 + 2 *3 / 4 % 5
x=2 1+6/4%5
1+1%5
Parenthesis
Power
Multiplication 1+1
Addition
Left to Right 2
29
29
Exercises
▰ What are the values of the following expressions?
▻ 10/3
▻ 5.2/2.0
9%3
▻
▰ What is the order of the following expression?
▻ X = 2 * 5 / 3+ 3 * 5 + 7
30
30
ITC121-Computer Programming 2 10
THANKS!
Any questions?
You can find me at
[email protected]
31
Learning Activities 1
Find the values or results of the following expressions. Assume that the variable X = 55 and the
variable Y = 12:
1. X&Y
2. X|Y
3. X^Y
4. X << 2
5. X << 3
6. Y >> 2
7. Y >> 3
8. Y << 3;
32
32
Learning Activities 2
Find the value of ANS. Assume that xInt = 8, yInt= 5, zInt = 3, ANS=6
1. ANS = xInt + zInt * yInt;
2. ANS = xInt - zInt * yInt % 3;
3. ANS -= zInt + yInt / xInt % 5;
4. ANS += yInt - zInt * xInt % 4;
5. ANS *= xInt / zInt * (yInt % 3);
6. ANS %= yInt / zInt * (xInt % 7);
7. ANS += xInt / zInt * yInt % 5;
8. ANS -= yInt / zInt + xInt % 5;
9. ANS /= xInt / zInt + (yInt % 6);
10. ANS *= xInt + zInt * yInt % 8;
33
33
ITC121-Computer Programming 2 11