operators and expressions n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Operators and Expressions PowerPoint Presentation
Download Presentation
Operators and Expressions

Loading in 2 Seconds...

play fullscreen
1 / 49

Operators and Expressions - PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on

FTSM. Operators and Expressions. Knowledge: Know the types of basic arithmetic operators and their order of precedence Skill: Develop skills in computer arithmetic. Introduction. We use arithmetic expressions to solve most programming problems

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
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

FTSM

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

operator and operand

Example:

W + Z

Operand

Operand

Operator

Operator and Operand

What are operator and operand?

TK1913-C Programming3

operator and operand1
Operator and Operand

Which is which?

Operator ??

  • Example:

A / B

Operand??

TK1913-C Programming4

basic arithmetic operators

Addition (+)

Modulus (%)

Subtraction (-)

Division (/)

Multiplication (*)

Basic Arithmetic Operators

TK1913-C Programming5

basic arithmetic operators1
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

division

Example:

W / Z

Integer Division

  • W and Z are integers

Floating Division

  • W or Z or both are floats
Division

TK1913-C Programming7

integer division

an integer

 the result is also an integer

an integer

Integer Division
  • Example:

8 / 2 = 4

TK1913-C Programming8

integer division1

an integer

 the result is also an integer

an integer

Integer Division
  • Example:

12 / 5 = 2

TK1913-C Programming9

floating division

a float

the result is a float

an integer

Floating Division
  • Example:

12.0 / 5 = 2

TK1913-C Programming10

slide11

Something to ponder …

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
Modulus
  • It returns the remainder that occurs after performing the division of 2 operands
  • Rule: Operands must be integers

TK1913-C Programming12

modulus1

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

modulus2

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

modulus3

a float

INVALID!

an integer

Modulus
  • Example:

12.0 % 3 = ??

TK1913-C Programming15

slide16

Something to ponder …

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

TK1913-C Programming16

arithmetic expression
Arithmetic Expression
  • An expression may contain 2 or more arithmetic operators
  • Main issue:

ORDER OF PRECEDENCE

TK1913-C Programming17

slide18

Arithmetic Expression

  • 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 expression1
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

arithmetic expression2

Example:

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

try it
Try it!
  • Example:

12 + 6.0 – 2 * 2 = ??

What’s the answer??

TK1913-C Programming21

arithmetic expression3
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 expression4
Arithmetic Expression
    • Example:
  • ( 9 – ( 3 + 2 ) ) * 3 = ??

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

– 5

12

4

TK1913-C Programming23

assignment statement
Assignment Statement

There are 3 types of assignment:

  • Simple
  • Multiple
  • Shorthand

TK1913-C Programming24

simple assignment

Don’t forget the semicolon !!

Simple Assignment

Syntax:

variable = expression ;

TK1913-C Programming25

simple assignment1

Buying price: 10.00

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

multiple assignment

Don’t forget the semicolon !!

Multiple Assignment

Syntax:

variable = variable = expression ;

TK1913-C Programming27

multiple assignment1

number

??

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
Shorthand Assignment

Syntax:

variableX = variableX op expression ;

variableX op= expression;

TK1913-C Programming29

shorthand assignment1

15

num

Shorthand Assignment

Whenever 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 assignment2

shorthand assignment operator

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

slide32

Shorthand Assignment

TK1913-C Programming32

shorthand assignment3

100.00

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
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
Relational Operators

TK1913-C Programming35

mantic logical operators
Mantic/Logical Operators

SymbolDescription

&& AND

|| OR

! NOT

TK1913-C Programming36

compound statement

a

b

c

d

2

5

15

17

Compound Statement

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

  • Example:
  • ! ( c > a )
  • ! ( 15 > 2 )

 ! ( 1 )

 0

TK1913-C Programming37

slide38

a

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

slide39

d

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

slide40

d

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
Increment and Decrement

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

decremented

TK1913-C Programming41

increment and decrement1

num

??

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
Prefix and Postfix

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

TK1913-C Programming43

prefix and postfix1

5

??

i

j

Prefix and Postfix
  • Example:
  • j = i++ - 2

similar to

j = i – 2;

i = i + 1;

6

3

TK1913-C Programming44

prefix and postfix2

5

??

i

j

Prefix and Postfix
  • Example:
  • j = ++i - 2

similar to

i = i + 1;

j = i – 2;

6

4

TK1913-C Programming45

slide46

10

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

slide47

15

total

2

number

??

7.000000

average

Coersion

    • Example:
    • int total, number;
  • float average;
  • average = total / number;
  • 15 / 2
  • 7

TK1913-C Programming47

slide48

15

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

end of lecture 6

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

CONTROL STRUCTURE: SELECTION on the way …

End of Lecture 6

TK1913-C Programming49