Sei sulla pagina 1di 49

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

FTSM

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 Programming

Operator and Operand


What are operator and operand?
Example:

W+Z
Operand

Operand
Operator
TK1913-C Programming

Operator and Operand


Which is which?

Operator ??
Example:

A/B

Operand??
TK1913-C Programming

Basic Arithmetic Operators

Addition (+)

Modulus (%)

Subtraction (-)
Division (/)
Multiplication (*)
TK1913-C Programming

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, lets study in detail the Division and Modulus


TK1913-C Programming

Division
Example:

W/Z

Integer Division

W and Z are integers

Floating Division

W or Z or both are
floats

TK1913-C Programming

Integer Division
Example:

8/2=4

an integer

the result is
an integer

TK1913-C Programming

also an integer
8

Integer Division
Example:

12 / 5 = 2

an integer

the result is
an integer

TK1913-C Programming

also an integer
9

Floating Division
Example:

12.0 / 5 = 2

a float

the result is a
float

an integer
TK1913-C Programming

10

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 Programming

11

Modulus

It returns the remainder that occurs


after performing the division of 2
operands

Rule: Operands must be integers

TK1913-C Programming

12

Modulus
Example:

12 % 5 = 2

an integer
result

2
5 12
10

the result is the


remainder of 12/5
remainder

an integer
TK1913-C Programming

13

Modulus
Example:

7%3=1

an integer
result

2
3 7
6

the result is the


remainder of 7/3
remainder

an integer
TK1913-C Programming

14

Modulus
Example:

12.0 % 3 = ??

a float

INVALID!

an integer
TK1913-C Programming

15

Something to ponder
The earlier expressions
contain only one
operator at a time. What
if the expression
contains more than one
operator?
TK1913-C Programming

16

Arithmetic Expression

An expression may contain 2 or more


arithmetic operators

Main issue:
ORDER OF PRECEDENCE

TK1913-C Programming

17

Arithmetic Expression
Examples:

5+6
5+6*2

= 11
= 17
22 or 17?

2.5 + 6 2 * 2

= ??
12 / 6.0 2 * 2 = ??
TK1913-C Programming

18

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 Programming

19

Arithmetic Expression
Example:

2.5 + 6 2 * 2 = 4.5
??
2.5 + 6 4
8.5 4
4.5

TK1913-C Programming

20

Try it!
Example:

12 + 6.0 2 * 2 = ??
Whats the answer??

TK1913-C Programming

21

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 Programming

22

Arithmetic Expression
Example:

12
(( 99 ((343 ++ 225)) )) ** 33 == ??
12

TK1913-C Programming

23

Assignment Statement
There are 3 types of assignment:

Simple

Multiple

Shorthand

TK1913-C Programming

24

Simple Assignment
Syntax:

variable = expression ;

Dont forget the semicolon !!


TK1913-C Programming

25

Buying
Buying price:
price: 10.00
10.00
Buying price: 10.00
_
Discount rate: 0.25
_
Discount rate: 0.25
_
Discount
rate: 0.25
For buying price RM10.00 and discount rate 0.25
For buying price RM10.00 _and discount rate 0.25
The total price is RM7.50
_
_

Simple Assignment
Example:

price 10.00
??
#include <stdio.h>
void main( ) {
discount 0.25
??
float price, discount, total;
printf(Buying price : );
total 7.50
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);
}
TK1913-C Programming

26

Multiple Assignment
Syntax:

variable = variable = expression ;

Dont forget the semicolon !!


TK1913-C Programming

27

Multiple Assignment
Example:

number

??
0

total

??
0

int number, total;


float start_x, start_y;

...

start_x 100.0
??

number = total = 0;

start_y 100.0
??

start_x = start_y = 100.0;

TK1913-C Programming

28

Shorthand Assignment
Syntax:

variableX = variableX op expression ;


variableX op= expression;

TK1913-C Programming

29

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

Example:
num = num + 5;

TK1913-C Programming

num

15
20

15 + 5
20

30

Shorthand Assignment
Expressions can also be stated using shorthand
assignment operators

Example:
num += 5;

similar to num = num + 5


shorthand assignment operator

Shorthand assignment operators have the lowest


order of precedence the last one to be evaluated
TK1913-C Programming

31

Shorthand Assignment
Operation

+=

Examples of
expression
num += 5;

num = num + 5;

-=

num -= 5;

num = num 5;

*=

num *= 5;

num = num * 5;

/=

num /= 5;

num = num / 5;

%=

num %= 5;

num = num % 5;

TK1913-C Programming

Description

32

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 Programming

pay

100.00
180.00

hour

rate

5.00
33

Assignment by Value
Every assignment expression has a value
Example:
int a, x, y, p;
Expression 1:
a = 1;
Line 1:
a=1;
Expression
2: x = y =x0;= y = 0;
Line 2:
Line
3:
Expression
3: p = 12;p = 12;
Line 4:
p = p +3;
p = 0;
Line 5:
q = p = p + x;
TK1913-C Programming

34

Relational Operators
Operation

Description

Examples
of
Expression
6<9

Value

1 (true)

<

Less than

<=

Less than or equal to

5 <= 5

1 (true)

>

Greater than

2>6

0 (false)

>=

Greater than or equal to

9 >= 5

1 (true)

==

Equal to

7 == 5

0 (false)

!=

Not equal to

6 != 5

1 (true)

TK1913-C Programming

35

Mantic/Logical Operators
Symbol
&&
||
!

Description
AND
OR
NOT

a && b

a || b

!a

!b

0
0

0
1

0
0

0
1

1
1

1
0

1
1

0
1

0
1

1
1

0
0

1
0

TK1913-C Programming

36

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

Example:

!(c>a)
! ( 15 > 2 )
!(1)
0
TK1913-C Programming

15

17
37

Compound Statement
Example:

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

1 && ( b == 5 )

1 && ( 5 == 5 )

15

17

1 && 1
1
TK1913-C Programming

38

Compound Statement
Example:

(c >= ( b * 3 ) ) || (a == 3)
( c >= ( 5 * 3 ) ) || ( a == 3)
( 15 >= 15 ) || ( a == 3)

1 || ( a == 3 )

1 || ( 2 == 3 )

15

1 || 0

17

1
TK1913-C Programming

39

Compound Statement
Example:

! ( ( a < b ) || ( c > d ) )
! ( ( 2 < 5 ) || ( c > d ) )
! ( 1 || ( c > d ) )

! ( 1 || ( 15 > 17 ) )

! ( 1 || 0 )

15

!1

17

0
TK1913-C Programming

40

Increment and Decrement


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

Description Examples of Description


Expression

++

Increment
operand by 1

i++

i=i+1

--

Decrement
operand by 1

i--

i=i1

TK1913-C Programming

41

Increment and Decrement


Example:
num 26
27
??
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);
Key-in
Key-in aa number:
number: 26
_
26
Value
before being incremented: 26
_
_
Value
after being incremented: 27 _
TK1913-C Programming

42

Prefix and Postfix


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

Description

i++

Value of i is incremented after being used in the


expression

++i

Value of i is incremented before being used in the


expression

i--

Value of i is decremented after being used in the


expression

--i

Value of i is decremented before being used in the


expression

TK1913-C Programming

43

Prefix and Postfix


Example:
j = i++ - 2
similar to

j = i 2;

5
6

i = i + 1;

??
3

TK1913-C Programming

44

Prefix and Postfix


Example:
j = ++i - 2
similar to

i = i + 1;

5
6

j = i 2;

??
4

TK1913-C Programming

45

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

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

10

??
y 10.000000

10.000000
TK1913-C Programming

46

Coersion
Example:
int total, number;
float average;

average = total / number;


15 / 2
7

total

15

number

??
average 7.000000
TK1913-C Programming

47

Coersion
Example:
int total, number;
float average;

average = (float) total / number;


15.000000 / 2
7.500000

total

15

number

??
average 7.500000
TK1913-C Programming

48

End of Lecture 6
Yes !! Thats all?
Whats next???

CONTROL STRUCTURE:
SELECTION on the way
TK1913-C Programming

49

Potrebbero piacerti anche