computer science department ftsm operators and expressions knowledge: know the types of basic...
TRANSCRIPT
Computer Science Department FTSMFTSM
Operators and Operators and ExpressionsExpressions
Knowledge:Know the types of basic arithmetic operators and their order of precedence
Skill:Develop skills in computer arithmetic
TK1913-C ProgrammingTK1913-C Programming 22
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
IntroductionIntroduction
TK1913-C ProgrammingTK1913-C Programming 33
Operator and OperandOperator and Operand
Example:Example:
W + ZW + Z
OperandOperand
OperatorOperator
OperandOperand
What are operator and operand?
TK1913-C ProgrammingTK1913-C Programming 44
Example:Example:
Operator and OperandOperator and OperandWhich is which?
A / BA / B
Operator ??Operator ??
Operand??Operand??
TK1913-C ProgrammingTK1913-C Programming 55
Basic Arithmetic OperatorsBasic Arithmetic Operators
Addition (+)Addition (+)
Subtraction (-)Subtraction (-)
Multiplication (*)Multiplication (*)
Division (/)Division (/)
Modulus (%)Modulus (%)
TK1913-C ProgrammingTK1913-C Programming 66
Basic Arithmetic OperatorsBasic Arithmetic Operators MultiplicationMultiplication, additionaddition and subtractionsubtraction are
the simplest to use
DivisionDivision is easy, but some precautions need to
be taken
ModulusModulus is the one that normally confuses
novices
So, let’s study in detail the DivisionDivision and ModulusModulus
TK1913-C ProgrammingTK1913-C Programming 77
DivisionDivisionExample:Example:
W / ZW / Z
Floating Division
WW or ZZ or both are
floats
Integer Division
WW and ZZ are integers
TK1913-C ProgrammingTK1913-C Programming 88
Example:Example:
Integer DivisionInteger Division
8 / 2 = 48 / 2 = 4
an integeran integer
an integeran integer
the result is the result is also an integeralso an integer
TK1913-C ProgrammingTK1913-C Programming 99
Example:Example:
Integer DivisionInteger Division
12 / 5 = 212 / 5 = 2
an integeran integer
an integeran integer
the result is the result is also an integeralso an integer
TK1913-C ProgrammingTK1913-C Programming 1010
Example:Example:
Floating DivisionFloating Division
12.0 / 5 = 212.0 / 5 = 2
a floata float
an integeran integer
the result is a the result is a floatfloat
TK1913-C ProgrammingTK1913-C Programming 1111
What will be the answer What will be the answer if an integer is divided if an integer is divided by 0? How about if one by 0? How about if one of the operands is a of the operands is a negative integer?negative integer?
Something to ponder …Something to ponder …
TK1913-C ProgrammingTK1913-C Programming 1212
ModulusModulus It returns the remainderremainder that occurs
after performing the division of 2 operands
Rule:Rule: Operands must be integersintegers
TK1913-C ProgrammingTK1913-C Programming 1313
Example:Example:
ModulusModulus
12 % 5 = 212 % 5 = 2
an integeran integer
an integeran integer
the result is the the result is the remainder of remainder of 12/512/5
1252
10
2
remainderresult
TK1913-C ProgrammingTK1913-C Programming 1414
Example:Example:
ModulusModulus
7 % 3 = 17 % 3 = 1
an integeran integer
an integeran integer
732
6
1
remainderresult
the result is the the result is the remainder of 7/3remainder of 7/3
TK1913-C ProgrammingTK1913-C Programming 1515
Example:Example:
ModulusModulus
12.0 % 3 = ??12.0 % 3 = ??
a floata float
an integeran integer
INVALID!INVALID!
TK1913-C ProgrammingTK1913-C Programming 1616
The earlier expressions The earlier expressions contain only one contain only one operator at a time. What operator at a time. What if the expression if the expression contains more than one contains more than one operator? operator?
Something to ponder …Something to ponder …
TK1913-C ProgrammingTK1913-C Programming 1717
An expression may contain 2 or more
arithmetic operators
Main issue:
ORDER OF PRECEDENCEORDER OF PRECEDENCE
Arithmetic ExpressionArithmetic Expression
TK1913-C ProgrammingTK1913-C Programming 1818
Examples:Examples:
5 + 65 + 6
5 + 6 * 25 + 6 * 2
2.5 + 6 – 2 * 22.5 + 6 – 2 * 2
12 / 6.0 – 2 * 212 / 6.0 – 2 * 2
= 11= 11
= 22 or 17?= 22 or 17?
= ??= ??
= ??= ??
= 17= 17
Arithmetic ExpressionArithmetic Expression
TK1913-C ProgrammingTK1913-C Programming 1919
Arithmetic ExpressionArithmetic ExpressionOrder of Precedence: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 ProgrammingTK1913-C Programming 2020
Example:Example:
Arithmetic ExpressionArithmetic Expression
2.5 + 6 – 2 * 2 = ??2.5 + 6 – 2 * 2 = ??
44
8.58.5
2.5 + 6 –2.5 + 6 –
– – 44
4.54.5
2.5 + 6 – 2 * 2 = 2.5 + 6 – 2 * 2 = Example:Example:2.5 + 6 – 2 * 2 = 4.52.5 + 6 – 2 * 2 = 4.5
44
8.58.5
2.5 + 6 –2.5 + 6 –
– – 44
4.54.5
2.5 + 6 – 2 * 2 = 2.5 + 6 – 2 * 2 =
TK1913-C ProgrammingTK1913-C Programming 2121
Example:Example:
Try it!Try it!
12 + 6.0 – 2 * 2 = ??12 + 6.0 – 2 * 2 = ??
What’s the answer??What’s the answer??
TK1913-C ProgrammingTK1913-C Programming 2222
Arithmetic ExpressionArithmetic 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:Example:
( 9 – ( 3 + 2 ) ) * 3 = ??( 9 – ( 3 + 2 ) ) * 3 = ??
TK1913-C ProgrammingTK1913-C Programming 2323
Arithmetic ExpressionArithmetic Expression
Example:
( 9 – ( 3 + 2 ) ) * 3 = ??( 9 – ( 3 + 2 ) ) * 3 = ??
Example:
( 9 – ( 3 + 2 ) ) * 3 = ??( 9 – ( 3 + 2 ) ) * 3 = ?? – – 5544 1212 ( 9 – ( 3 + 2 ) ) * 3 = 12( 9 – ( 3 + 2 ) ) * 3 = 12
TK1913-C ProgrammingTK1913-C Programming 2424
Assignment StatementAssignment Statement
There are 3 types of assignment:
Simple
Multiple
Shorthand
TK1913-C ProgrammingTK1913-C Programming 2525
Simple AssignmentSimple AssignmentSyntax:
variable = expression ;variable = expression ;
Don’t forget the semicolon !!Don’t forget the semicolon !!
TK1913-C ProgrammingTK1913-C Programming 2626
Simple AssignmentSimple 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);
}
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);
}
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
_
TK1913-C ProgrammingTK1913-C Programming 2727
Syntax:
variable = variable = expression ;variable = variable = expression ;
Multiple AssignmentMultiple Assignment
Don’t forget the semicolon !!Don’t forget the semicolon !!
TK1913-C ProgrammingTK1913-C Programming 2828
Example:
int number, total;
float start_x, start_y;
. . .
number = total = 0;
start_x = start_y = 100.0;
Example:
int number, total;
float start_x, start_y;
. . .
number = total = 0;
start_x = start_y = 100.0;
total ??
number ??
start_x ??
start_y ??
0
0
100.0
100.0
Multiple AssignmentMultiple Assignment
TK1913-C ProgrammingTK1913-C Programming 2929
Shorthand AssignmentShorthand Assignment
Syntax:
variableX = variableX variableX = variableX opop expression ; expression ;
variableX variableX opop= expression;= expression;
TK1913-C ProgrammingTK1913-C Programming 3030
Whenever the expression on the right contains the variable on the left (to which the value is assigned)
Example:
num = num + 5;
Example:
num = num + 5;
num 15
15 + 515 + 5
2020
20
Shorthand AssignmentShorthand Assignment
TK1913-C ProgrammingTK1913-C Programming 3131
Expressions can also be stated using shorthand assignment operators
Example:
num += 5;
Example:
num += 5; similar to similar to num = num + 5num = num + 5
shorthand assignment operatorshorthand assignment operator
Shorthand AssignmentShorthand Assignment
Shorthand assignment operators have the lowest order of precedence – the last one to be evaluated
TK1913-C ProgrammingTK1913-C Programming 3232
OperationOperation Examples of Examples of expressionexpression
DescriptionDescription
+= 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 AssignmentShorthand Assignment
TK1913-C ProgrammingTK1913-C Programming 3333
Example:
pay += hour * rate * 2
Example:
pay += hour * rate * 2
pay + (8 * 5.00 * 2)pay + (8 * 5.00 * 2)
pay 100.00
hour 8
rate 5.00
similar to similar to pay = pay + (hour * rate * 2)pay = pay + (hour * rate * 2)
pay + (hour * rate * 2)pay + (hour * rate * 2)
100.00 + 80.00100.00 + 80.00 180.00180.00
180.00
Shorthand AssignmentShorthand Assignment
TK1913-C ProgrammingTK1913-C Programming 3434
Assignment by ValueAssignment by ValueEvery assignment expression has a value
Example:
Expression 1:Expression 1: a = 1;a = 1;
Expression 2:Expression 2: x = y = 0;x = y = 0;
Expression 3:Expression 3: p = 12;p = 12;
p = 0;p = 0;
Example:
Expression 1:Expression 1: a = 1;a = 1;
Expression 2:Expression 2: x = y = 0;x = y = 0;
Expression 3:Expression 3: p = 12;p = 12;
p = 0;p = 0;
Example: int a, x, y, p;int a, x, y, p;Line 1:Line 1: a=1;a=1;Line 2:Line 2: x = y = 0;x = y = 0;Line 3:Line 3: p = 12;p = 12;Line 4:Line 4: p = p +3;p = p +3;Line 5:Line 5: q = p = p + x;q = p = p + x;
Example: int a, x, y, p;int a, x, y, p;Line 1:Line 1: a=1;a=1;Line 2:Line 2: x = y = 0;x = y = 0;Line 3:Line 3: p = 12;p = 12;Line 4:Line 4: p = p +3;p = p +3;Line 5:Line 5: q = p = p + x;q = p = p + x;
TK1913-C ProgrammingTK1913-C Programming 3535
Relational OperatorsRelational OperatorsOperationOperation DescriptionDescription Examples Examples
of of ExpressionExpression
ValueValue
< 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)
TK1913-C ProgrammingTK1913-C Programming 3636
Mantic/Logical OperatorsMantic/Logical OperatorsSymbol Description
&&&& ANDAND
|||| OROR
!! NOTNOT
aa bb a && ba && b a || ba || b !a!a !b!b0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0
TK1913-C ProgrammingTK1913-C Programming 3737
Compound StatementCompound StatementArithmetic, relational and mantic operators can be
integrated/combined in one expression
Example:
! ( c > a )
Example:
! ( c > a )a 2
b 5
c 15
d 17 ! ( 1 )! ( 1 )
! ( 15 > 2 )! ( 15 > 2 )
00
TK1913-C ProgrammingTK1913-C Programming 3838
Example:
(a >= 1) && (b == 5)
Example:
(a >= 1) && (b == 5)
a 2
b 5
c 15
d 17
( 2 >= 1 ) && ( b == 5 )( 2 >= 1 ) && ( b == 5 )
Compound StatementCompound Statement
1 && ( b == 5 )1 && ( b == 5 ) 1 && ( 5 == 5 )1 && ( 5 == 5 ) 1 && 11 && 1 11
TK1913-C ProgrammingTK1913-C Programming 3939
Example:
(c >= ( b * 3 ) ) || (a == 3)
Example:
(c >= ( b * 3 ) ) || (a == 3)
a 2
b 5
c 15
d 17
( c >= ( 5 * 3 ) ) || ( a == 3)( c >= ( 5 * 3 ) ) || ( a == 3)
Compound StatementCompound Statement
1 || ( a == 3 )1 || ( a == 3 ) 1 || ( 2 == 3 )1 || ( 2 == 3 ) 1 || 01 || 0
( 15 >= 15 ) || ( a == 3)( 15 >= 15 ) || ( a == 3)
11
TK1913-C ProgrammingTK1913-C Programming 4040
Example:
! ( ( a < b ) || ( c > d ) )
Example:
! ( ( a < b ) || ( c > d ) )
a 2
b 5
c 15
d 17
! ( ( 2 < 5 ) || ( c > d ) )! ( ( 2 < 5 ) || ( c > d ) )
Compound StatementCompound Statement
! ( 1 || ( 15 > 17 ) )! ( 1 || ( 15 > 17 ) ) ! ( 1 || 0 )! ( 1 || 0 ) ! 1 ! 1
! ( 1 || ( c > d ) )! ( 1 || ( c > d ) )
00
TK1913-C ProgrammingTK1913-C Programming 4141
Increment and DecrementIncrement and DecrementThis operation contains only one operand, that is, the operand which value will be incremented/decremented
SymbolSymbol DescriptionDescription Examples of Examples of ExpressionExpression
DescriptionDescription
++ Increment operand by 1
i++ i = i + 1
-- Decrement operand by 1
i-- i = i – 1
TK1913-C ProgrammingTK1913-C Programming 4242
Key-in a number: _Key-in a number: 26
_
Increment and DecrementIncrement 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);
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);
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 _
TK1913-C ProgrammingTK1913-C Programming 4343
Prefix and PostfixPrefix and PostfixIncrement and Decrement operators can be either in prefix or postfix forms
ExpressionExpression DescriptionDescription
i++ Value of ii is incrementedincremented afterafter being used in the expression
++i Value of ii is incrementedincremented beforebefore being used in the expression
i-- Value of ii is decrementeddecremented afterafter being used in the expression
--i Value of ii is decrementeddecremented beforebefore being used in the expression
TK1913-C ProgrammingTK1913-C Programming 4444
Example:
j = i++ - 2
Example:
j = i++ - 2
i 5
similar to similar to
j = i – 2;j = i – 2;
i = i + 1;i = i + 1;
Prefix and PostfixPrefix and Postfix
j ??3
6
TK1913-C ProgrammingTK1913-C Programming 4545
Example:
j = ++i - 2
Example:
j = ++i - 2
i 5
similar to similar to
i = i + 1;i = i + 1;
j = i – 2;j = i – 2;
Prefix and PostfixPrefix and Postfix
j ??4
6
TK1913-C ProgrammingTK1913-C Programming 4646
Example: int x = 10;
float y;y = (float) x;
Example: int x = 10;
float y;y = (float) x;
CoersionCoersion
( float ) 10( float ) 10 10.00000010.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)
TK1913-C ProgrammingTK1913-C Programming 4747
Example: int total, number;
float average;…average = total / number;
Example: int total, number;
float average;…average = total / number;
CoersionCoersion
15 / 215 / 2 77
total 15
number 2
average ??7.000000
TK1913-C ProgrammingTK1913-C Programming 4848
Example: int total, number;
float average;…average = (float) total / number;
Example: int total, number;
float average;…average = (float) total / number;
CoersionCoersion
15.000000 / 215.000000 / 2 7.5000007.500000
total 15
number 2
average ??7.500000
TK1913-C ProgrammingTK1913-C Programming 4949
End of Lecture 6End of Lecture 6
Yes !! That’s all? Yes !! That’s all? What’s next???What’s next???
CONTROL STRUCTURE: CONTROL STRUCTURE: SELECTIONSELECTION on the way … on the way …