- 138 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Operators and Expressions' - arden-glenn

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### Operators and Expressions

Knowledge:

Know the types of basic arithmetic operators and their order of precedence

Skill:

Develop skills in computer arithmetic

Computer Science Department

Introduction

- We use arithmetic expressions to solve most programming problems
- Arithmetic expressions comprise of operators and operands
- There are rules for writing and evaluating arithmetic expressions

TK1913-C Programming2

Modulus (%)

Subtraction (-)

Division (/)

Multiplication (*)

Basic Arithmetic OperatorsTK1913-C Programming5

Basic Arithmetic Operators

- Multiplication, addition and subtraction are the simplest to use
- Division is easy, but some precautions need to be taken
- Modulus is the one that normally confuses novices

So, let’s study in detail the Division and Modulus

TK1913-C Programming6

What will be the answer if an integer is divided by 0? How about if one of the operands is a negative integer?

TK1913-C Programming11

Modulus

- It returns the remainder that occurs after performing the division of 2 operands
- Rule: Operands must be integers

TK1913-C Programming12

the result is the remainder of 12/5

an integer

result

remainder

an integer

Modulus- Example:

12 % 5 = 2

2

5

12

10

2

TK1913-C Programming13

the result is the remainder of 7/3

an integer

result

remainder

an integer

Modulus- Example:

7 % 3 = 1

2

3

7

6

1

TK1913-C Programming14

The earlier expressions contain only one operator at a time. What if the expression contains more than one operator?

TK1913-C Programming16

Arithmetic Expression

- An expression may contain 2 or more arithmetic operators
- Main issue:

ORDER OF PRECEDENCE

TK1913-C Programming17

- Examples:

5 + 6

= 11

5 + 6 * 2

= 17

= 22 or 17?

2.5 + 6 – 2 * 2

= ??

= ??

12 / 6.0 – 2 * 2

TK1913-C Programming18

Arithmetic Expression

Order of Precedence:

High:* / %

Low:+ -

All operators have a precedence level. High precedence level operators are evaluated before lower ones. Operators of the same precedence level are evaluated from left to right

TK1913-C Programming19

2.5 + 6 – 2 * 2 =

2.5 + 6 – 2 * 2 = 4.5

2.5 + 6 –

4

8.5

– 4

4.5

Arithmetic Expression- Example:

2.5 + 6 – 2 * 2 = ??

2.5 + 6 – 2 * 2 =

2.5 + 6 –

4

8.5

– 4

4.5

TK1913-C Programming20

Arithmetic Expression

- All expressions in parentheses (brackets) must be evaluated prior to values outside brackets
- Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first

- Example:

( 9 – ( 3 + 2 ) ) * 3 = ??

TK1913-C Programming22

Arithmetic Expression

- Example:
- ( 9 – ( 3 + 2 ) ) * 3 = ??

( 9 – ( 3 + 2 ) ) * 3 = 12

– 5

12

4

TK1913-C Programming23

Discount rate: 0.25

For buying price RM10.00 and discount rate 0.25

The total price is RM7.50

_

Buying price: 10.00

Discount rate: _

Buying price: 10.00

Discount rate: 0.25

_

Buying price: 10.00

_

Buying price: 10.00

Discount rate: 0.25

For buying price RM10.00 and discount rate 0.25

_

Buying price: _

price

10.00

??

??

discount

0.25

total

??

Simple Assignment- Example:
- #include <stdio.h>
- void main( ) {
- float price, discount, total;
- printf(“Buying price : “);
- scanf(“%f”, &price);
- printf(“\nDiscount rate : “);
- scanf(“%f”, &discount);
- total = price – (price * discount);
- printf(“\nFor buying price RM%.2f and discount rate %.2f\n”, price, discount);
- printf(“The total price is RM%.2f\n”, total);
- }

7.50

TK1913-C Programming26

??

0

??

0

total

start_x

??

100.0

start_y

??

100.0

Multiple Assignment- Example:
- int number, total;
- float start_x, start_y;
- . . .
- number = total = 0;
- start_x = start_y = 100.0;

TK1913-C Programming28

Shorthand Assignment

Syntax:

variableX = variableX op expression ;

variableX op= expression;

TK1913-C Programming29

num

Shorthand AssignmentWhenever the expression on the right contains the variable on the left (to which the value is assigned)

- Example:
- num = num + 5;

20

15 + 5

20

TK1913-C Programming30

Shorthand Assignment

Expressions can also be stated using shorthand assignment operators

- Example:
- num += 5;

similar to num = num + 5

Shorthand assignment operators have the lowest order of precedence – the last one to be evaluated

TK1913-C Programming31

TK1913-C Programming32

8

5.00

pay

hour

rate

180.00

Shorthand Assignment- Example:
- pay += hour * rate * 2

similar to pay = pay + (hour * rate * 2)

- pay + (hour * rate * 2)

pay + (8 * 5.00 * 2)

100.00 + 80.00

180.00

TK1913-C Programming33

Assignment by Value

Every assignment expression has a value

- Example:
- Expression 1: a = 1;
- Expression 2: x = y = 0;
- Expression 3: p = 12;
- p = 0;

- Example:
- int a, x, y, p;
- Line 1: a=1;
- Line 2: x = y = 0;
- Line 3: p = 12;
- Line 4: p = p +3;
- Line 5: q = p = p + x;

TK1913-C Programming34

Relational Operators

TK1913-C Programming35

b

c

d

2

5

15

17

Compound StatementArithmetic, relational and mantic operators can be integrated/combined in one expression

- Example:
- ! ( c > a )

- ! ( 15 > 2 )

! ( 1 )

0

TK1913-C Programming37

b

c

d

2

5

15

17

Compound Statement

- Example:
- (a >= 1) && (b == 5)

- ( 2 >= 1 ) && ( b == 5 )

- 1 && ( b == 5 )

- 1 && ( 5 == 5 )

- 1 && 1

- 1

TK1913-C Programming38

a

b

c

2

5

15

17

Compound Statement

- Example:
- (c >= ( b * 3 ) ) || (a == 3)

- ( c >= ( 5 * 3 ) ) || ( a == 3)

- ( 15 >= 15 ) || ( a == 3)

- 1 || ( a == 3 )

- 1 || ( 2 == 3 )

- 1 || 0

- 1

TK1913-C Programming39

a

b

c

2

5

15

17

Compound Statement

- Example:
- ! ( ( a < b ) || ( c > d ) )

- ! ( ( 2 < 5 ) || ( c > d ) )

- ! ( 1 || ( c > d ) )

- ! ( 1 || ( 15 > 17 ) )

- ! ( 1 || 0 )

- ! 1

- 0

TK1913-C Programming40

Increment and Decrement

This operation contains only one operand, that is, the operand which value will be incremented/

decremented

TK1913-C Programming41

??

Increment and Decrement- Example:
- int num;
- printf(“Key-in a number: “);
- scanf(“%d”, &num);
- printf(“Value before being incremented: %d\n”, num);
- num++;
- printf(“Value after being incremented: %d”, num);

26

27

Key-in a number: 26

Value before being incremented: 26

_

Key-in a number: 26

Value before being incremented: 26

Value after being incremented: 27 _

Key-in a number: _

Key-in a number: 26

_

TK1913-C Programming42

Prefix and Postfix

Increment and Decrement operators can be either in prefix or postfix forms

TK1913-C Programming43

x

??

y

10.000000

Coersion

Coersion is used to compute a value that is equivalent to its operand’s value (based on the stated data type)

- Example:
- int x = 10;
- float y;
- y = (float) x;

- ( float ) 10

- 10.000000

TK1913-C Programming46

total

2

number

??

7.000000

average

Coersion

- Example:
- int total, number;
- float average;
- …
- average = total / number;

- 15 / 2

- 7

TK1913-C Programming47

total

2

number

??

7.500000

average

Coersion

- Example:
- int total, number;
- float average;
- …
- average = (float) total / number;

- 15.000000 / 2

- 7.500000

TK1913-C Programming48

Yes !! That’s all? What’s next???

CONTROL STRUCTURE: SELECTION on the way …

End of Lecture 6TK1913-C Programming49

Download Presentation

Connecting to Server..