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

Week2-Operators

The document provides an overview of operators in C#, including arithmetic, relational, logical, bitwise, and assignment operators. It explains how these operators manipulate variables and values, along with examples of their usage and precedence rules. Additionally, it includes exercises for practicing the application of these concepts.

Uploaded by

gavilankim2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week2-Operators

The document provides an overview of operators in C#, including arithmetic, relational, logical, bitwise, and assignment operators. It explains how these operators manipulate variables and values, along with examples of their usage and precedence rules. Additionally, it includes exercises for practicing the application of these concepts.

Uploaded by

gavilankim2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

ITC121

Computer Programming 2

Operators
1

Operators Overview

▰ Operators are symbols that tell the compiler to


perform operations on operands.
▰ Operators are used to manipulate variables and
values in a program.
▰ C# has a rich set of built-in operators like arithmetic
operators, relational operators, logical operators,
bitwise operators, and assignment operators.
2

Arithmetic Operators
Assume variable A
holds 10 and
variable B holds
20 then:

ITC121-Computer Programming 2 1
Arithmetic (cont…)

▰ The five most commonly used binary arithmetic operators

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

Mixing Integer and Floating


• When you perform an operation where one operand is an
integer and the other operand is a floating point the result
is a floating point
• The integer is converted to a floating point before the
operation
99 / 100 = 0
99 / 100.0 = 0.99
99.0 / 100 = 0.99
1 + 2 * 3 / 4.0 – 5 = -2.5
6

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:

OPERATOR INITIAL VALUE EXAMPLE RESULT


+= int sum = 10; sum += 30; sum = 40
-= int year = 2019; year -= 1985; year = 34
*= int salary = 22938; salary *= 12; salary = 275256
/= int income = 15000 income /= 30 income = 500
%= int number = 45; number %= 10 number = 5
++ int counter = 10; counter++ counter =11
-- int x = 30; x-- x = 29

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 ?:

Assignment =, *=, /=, %=, +=, -=, <<=, >>=,


&=, ^=, |=

21

21

ITC121-Computer Programming 2 7
Operator Precedence Rules

Highest precedence rule to lowest precedence rule:


 Parenthesis are always respected
 Exponentiation (raise to a power) Parenthesis
Power
 Multiplication, Division, and Remainder Multiplication
Addition
 Addition and Subtraction
Left to Right
 Left to right

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

You might also like