Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
TK1913-C Programming
W+Z
Operand
Operand
Operator
TK1913-C Programming
Operator ??
Example:
A/B
Operand??
TK1913-C Programming
Addition (+)
Modulus (%)
Subtraction (-)
Division (/)
Multiplication (*)
TK1913-C Programming
Division
Example:
W/Z
Integer Division
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
TK1913-C Programming
12
Modulus
Example:
12 % 5 = 2
an integer
result
2
5 12
10
an integer
TK1913-C Programming
13
Modulus
Example:
7%3=1
an integer
result
2
3 7
6
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
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:
* / %
+ -
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
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 ;
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:
27
Multiple Assignment
Example:
number
??
0
total
??
0
...
start_x 100.0
??
number = total = 0;
start_y 100.0
??
TK1913-C Programming
28
Shorthand Assignment
Syntax:
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;
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
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
<=
5 <= 5
1 (true)
>
Greater than
2>6
0 (false)
>=
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
operand by 1
i++
i=i+1
--
Decrement
operand by 1
i--
i=i1
TK1913-C Programming
41
42
Description
i++
++i
i--
--i
TK1913-C Programming
43
j = i 2;
5
6
i = i + 1;
??
3
TK1913-C Programming
44
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;
total
15
number
??
average 7.000000
TK1913-C Programming
47
Coersion
Example:
int total, number;
float average;
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