Slide 06 - Operators and Expressions
-
Upload
effendyfooad -
Category
Documents
-
view
228 -
download
0
Transcript of Slide 06 - Operators and Expressions
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 1/49
Computer Science Department FTSM
Operators andExpressions
Knowledge:
Know the types of basic arithmetic operators and their order of precedence
Skill:
Develop skills in computer arithmetic
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 2/49
TK1913-C Programming 2
We use arithmetic expressions to solve
most programming problems
Arithmetic expressions comprise of
operators and operands
There are rules for writing andevaluating arithmetic expressions
Introduction
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 3/49
TK1913-C Programming 3
Operator and Operand
Example:
W + Z
Operand
Operator
Operand
What are operator and operand?
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 4/49
TK1913-C Programming 4
Example:
Operator and OperandWhich is which?
A / B
Operator ??
Operand??
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 5/49
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 6/49
TK1913-C Programming 6
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
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 7/49
TK1913-C Programming 7
DivisionExample:
W / Z
Floating Division
W or Z or both are
floats
Integer Division
W and Z are integers
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 8/49
TK1913-C Programming 8
Example:Integer Division
8 / 2 = 4
an integer
an integer
the result isalso an integer
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 9/49
TK1913-C Programming 9
Example:Integer Division
12 / 5 = 2
an integer
an integer
the result isalso an integer
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 10/49
TK1913-C Programming 10
Example:Floating Division
12.0 / 5 = 2
a float
an integer
the result is a
float
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 11/49
TK1913-C Programming 11
What will be the answer
if an integer is dividedby 0? How about if one
of the operands is a
negative integer?
Something to ponder …
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 12/49
TK1913-C Programming 12
Modulus
It returns the remainder that occurs
after performing the division of 2
operands
Rule: Operands must be integers
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 13/49
TK1913-C Programming 13
Example:
Modulus
12 % 5 = 2
an integer
an integer
the result is theremainder of 12/5
125
2
102
remainderresult
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 14/49
TK1913-C Programming 14
Example:
Modulus
7 % 3 = 1
an integer
an integer
73
2
61
remainderresult
the result is theremainder of 7/3
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 15/49
TK1913-C Programming 15
Example:
Modulus
12.0 % 3 = ??
a float
an integer
INVALID!
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 16/49
TK1913-C Programming 16
The earlier expressions
contain only oneoperator at a time. What
if the expression
contains more than one
operator?
Something to ponder …
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 17/49
TK1913-C Programming 17
An expression may contain 2 or more
arithmetic operators
Main issue:
ORDER OF PRECEDENCE
Arithmetic Expression
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 18/49
TK1913-C Programming 18
Examples:
5 + 6
5 + 6 * 2
2.5 + 6 – 2 * 2
12 / 6.0 – 2 * 2
= 11
= 22 or 17?
= ??
= ??
= 17
Arithmetic Expression
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 19/49
TK1913-C Programming 19
Arithmetic ExpressionOrder of Precedence:
High: * / %
Low: + -
All operators have a precedence level. High
precedence level operators are evaluated beforelower ones. Operators of the same precedence levelare evaluated from left to right
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 20/49
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 21/49
TK1913-C Programming 21
Example:
Try it!
12 + 6.0 – 2 * 2 = ??
What’s the answer??
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 22/49
TK1913-C Programming 22
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 = ??
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 23/49
TK1913-C Programming 23
Arithmetic Expression
Example:
( 9 – ( 3 + 2 ) ) * 3 = ?? – 54 12( 9 – ( 3 + 2 ) ) * 3 = 12
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 24/49
TK1913-C Programming 24
Assignment Statement
There are 3 types of assignment:
Simple
Multiple
Shorthand
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 25/49
Buying price:
Buying price: 10 00
Buying price: 10 00
Buying price: 10 00
Buying price: 10 00
Buying price: 10.00
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 26/49
TK1913-C Programming 26
Simple AssignmentExample:
#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);
}
Buying price: _
discount ??
price ??
total ??
Buying price: 10.00
_
10.00
Buying price: 10.00
Discount rate: _
Buying price: 10.00
Discount rate: 0.25
_
0.25
7.50
Buying price: 10.00
Discount rate: 0.25
For buying price RM10.00 and discount rate 0.25
_
Buying price: 10.00
Discount rate: 0.25
For buying price RM10.00 and discount rate 0.25
The total price is RM7.50
_
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 27/49
TK1913-C Programming 27
Syntax:
variable = variable = expression ;
Multiple Assignment
Don’t forget the semicolon !!
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 28/49
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 29/49
TK1913-C Programming 29
Shorthand Assignment
Syntax:
variableX = variableX op expression ;
variableX op = expression;
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 30/49
TK1913-C Programming 30
Whenever the expression on the right contains the
variable on the left (to which the value is assigned)
Example:
num = num + 5;
num 15
15 + 5
20
20
Shorthand Assignment
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 31/49
TK1913-C Programming 31
Expressions can also be stated using shorthand
assignment operators
Example:num += 5; similar to num = num + 5
shorthand assignment operator
Shorthand Assignment
Shorthand assignment operators have the lowest
order of precedence – the last one to be evaluated
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 32/49
TK1913-C Programming 32
Operation Examples of
expression
Description
+= 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;
Shorthand Assignment
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 33/49
TK1913-C Programming 33
Example:
pay += hour * rate * 2
pay + (8 * 5.00 * 2)
pay 100.00
hour 8
rate 5.00
similar to pay = pay + (hour * rate * 2)
pay + (hour * rate * 2)
100.00 + 80.00
180.00
180.00
Shorthand Assignment
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 34/49
TK1913-C Programming 34
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;
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 35/49
TK1913-C Programming 35
Relational OperatorsOperation Description Examples
of
Expression
Value
< Less than 6 < 9 1 (true)<= 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)
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 36/49
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 37/49
TK1913-C Programming 37
Compound Statement
Arithmetic, relational and mantic operators can be
integrated/combined in one expression
Example:
! ( c > a )a 2
b 5
c 15
d 17! ( 1 )
! ( 15 > 2 )
0
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 38/49
TK1913-C Programming 38
Example:
(a >= 1) && (b == 5)
a 2
b 5
c 15
d 17
( 2 >= 1 ) && ( b == 5 )
Compound Statement
1 && ( b == 5 )
1 && ( 5 == 5 )
1 && 1
1
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 39/49
TK1913-C Programming 39
Example:
(c >= ( b * 3 ) ) || (a == 3)
a 2
b 5
c 15
d 17
( c >= ( 5 * 3 ) ) || ( a == 3)
Compound Statement
1 || ( a == 3 )
1 || ( 2 == 3 )
1 || 0
( 15 >= 15 ) || ( a == 3)
1
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 40/49
TK1913-C Programming 40
Example:
! ( ( a < b ) || ( c > d ) )
a 2
b 5
c 15
d 17
! ( ( 2 < 5 ) || ( c > d ) )
Compound Statement
! ( 1 || ( 15 > 17 ) )
! ( 1 || 0 )
! 1
! ( 1 || ( c > d ) )
0
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 41/49
TK1913-C Programming 41
Increment and DecrementThis operation contains only one operand, that is,the operand which value will be incremented/
decremented
Symbol Description Examples of
Expression
Description
++ Increment
operand by 1
i++ i = i + 1
-- Decrement
operand by 1
i-- i = i – 1
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 42/49
TK1913-C Programming 42
Key-in a number: _Key-in a number: 26
_
Increment and DecrementExample:
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);
num ??
Key-in a number: 26
Value before being incremented: 26
_
2627
Key-in a number: 26
Value before being incremented: 26
Value after being incremented: 27 _
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 43/49
TK1913-C Programming 43
Prefix and PostfixIncrement 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 theexpression
--i Value of i is decremented before being used in the
expression
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 44/49
TK1913-C Programming 44
Example:
j = i++ - 2
i 5
similar to
j = i – 2;
i = i + 1;
Prefix and Postfix
j ??3
6
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 45/49
TK1913-C Programming 45
Example:
j = ++i - 2
i 5
similar to
i = i + 1;
j = i – 2;
Prefix and Postfix
j ??4
6
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 46/49
TK1913-C Programming 46
Example:int x = 10;
float y;
y = (float) x;
Coersion
( float ) 10
10.000000
x 10
y ??10.000000
Coersion is used to compute a value that is equivalent
to its operand’s value (based on the stated data type)
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 47/49
TK1913-C Programming 47
Example:
int total, number;
float average;
…
average = total / number;
Coersion
15 / 2
7
total 15
number 2
average ??7.000000
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 48/49
TK1913-C Programming 48
Example:
int total, number;
float average;
…
average = (float) total / number;
Coersion
15.000000 / 2
7.500000
total 15
number 2
average ??7.500000
8/10/2019 Slide 06 - Operators and Expressions
http://slidepdf.com/reader/full/slide-06-operators-and-expressions 49/49
End of Lecture 6
Yes !! That’s all?What’s next???
CONTROL STRUCTURE:
SELECTION on the way …