lecture4 basic c operators
Post on 13-Jan-2016
218 Views
Preview:
DESCRIPTION
TRANSCRIPT
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 1/18
Principles of Programming - NI July2005 1
Lecture 4: Basic C Operators
In this chapter, you will learn about:
Arithmetic operators
Unary operators
Binary operators Assignment operators
Equalities and relational operators
Logical operators
Conditional operator
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 2/18
Principles of Programming - NI July2005 2
Arithmetic Operators I
In C, we have the ollowing operators !notethat all these e"ample are using 9 as thevalue o its irst operand#
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 3/18
Principles of Programming - NI July2005 3
Arithmetic Operators II
$here are % types o arithmetic operatorsin C:
unary operators
operators that require only one operand&binary operators&
operators that require two operands&
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 4/18
Principles of Programming - NI July2005 4
Unary Operator
C 'peration 'perator E"ample
(ositive ) a * )+
egative - b * -a
Increment )) i)).ecrement -- i--
$he irst assigns positive + to a
$he second assigns the negative value o a to b&i)) is equivalent to i * i ) /
i-- is equivalent to i * i-/
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 5/18
Principles of Programming - NI July2005 5
PRE- / POST-Increment
It is also possible to use ))i and --i instead oi)) and i--
0owever, the two orms have a slightly yetimportant dierence&
Consider this e"ample:int a = 9;
printf(“%d\n”, a++);
printf(“%d”, a);
$he output would be:
1
/2
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 6/18
Principles of Programming - NI July2005 6
PRE- / POST-Increment cont
But i we have:
int a = 9;
printf(“%d\n”, ++a);
printf(“%d”, a);$he output would be:
/2
/2
a)) would return the current value o a andthen increment the value o a
))a on the other hand increment the value oa beore returning the value
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 7/18
Principles of Programming - NI July2005 7
The !o""o#in$ ta%"e i""ustrates the &i'erence %et#een the pre() an&post() mo&es o! the increment an& &ecrement operator*
int R + ,.count+,
)) 'r --3tatement
Equivalent3tatements 4 value Count
value
4 * count))5 4 * count5count * count ) / /2 //
4 * ))count5 count * count ) /54 * count5
// //
4 * count --5 4 * count5count * count 6 /5
/2 1
4 * --count5 Count * count 6 /54 * count5
1 1
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 8/18
Principles of Programming - NI July2005 8
Binary Operators
C Operation Operator Example:
Addition ) a ) +
3ubtraction - a - 7
8ultiplication 9 a 9 b.ivision a c
8odulus ; a ; "
$he division o variables o type int will alwaysproduce a variable o type int as the result&
<ou could only use modulus !;# operationon int variables&
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 9/18
Principles of Programming - NI July2005 9
Assi$nment Operators
Assignment operators are used to combine the=*= operator with one o the binary arithmeticoperators
In the ollowing slide, All operations starting romc * 1
Operator Example EquivalentStatement
Results
)* c )* > c * c ) > c * /7
-* c -* ? c * c 6 ? c * /9* c 9* /2 c * c 9 /2 c * 12
* c * @ c * c @ c * /
;* c ;* @ c * c ; @ c *
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 10/18
Principles of Programming - NI July2005 10
Prece&ence Ru"es
(recedence rules come into play when there is a mi"edo arithmetic operators in one statement& or e"ample:" * + 9 a - ))b;+5
$he rules speciy which o the operators will beevaluated irst&
Precedence Operator AssociativityLevel
/ !highest# !# let to right
% unary right to let+ 9 ; let to right
) - let to right
@ !lowest# * )* -* 9* * ;* right to let
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 11/18
Principles of Programming - NI July2005 11
Prece&ence Ru"es cont
or e"ample: " * + 9 a - ))b ; +5how would this statement be evaluated?
I we intend to have the statement evaluateddierently rom the way speciied by the
precedence rules, we need to speciy it usingparentheses ! #
Using parenthesis, we will have
" * + 9 !!a - ))b#;+#5
$he e"pression inside a parentheses will beevaluated irst&
$he inner parentheses will be evaluatedearlier compared to the outer parentheses&
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 12/18
Principles of Programming - NI July2005 12
E0ua"ity an& Re"ationa"
OperatorsEquality 'perators: Operator Example Meaning
** " ** y " is equal to y
* " * y " is not equal to y
4elational 'perators: Operator Example Meaning
D " D y " is greater than y
" y " is less than y
D* " D* y " is greater than or equal to y* " * y " is less than or equal to y
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 13/18
Principles of Programming - NI July2005 13
Lo$ica" Operators
Logical operators are useul when we want to testmultiple conditions&
$here are + types o logical operators and they
worF the same way as the boolean A., '4 and'$ operators&
GG - Logical A.
All the conditions must be true or the whole
e"pression to be true&E"ample: i !a ** /2 GG b ** 1 GG d ** /#
means the if statement is only true when a **/2 and b ** 1 and d ** /&
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 14/18
Principles of Programming - NI July2005 14
Lo$ica" Operators cont
HH - Logical '4
$he truth o one condition is enough to maFethe whole e"pression true&
E"ample: i !a ** /2 HH b ** 1 HH d ** /#
means the if statement is true when eiterone o a, b or d has the right value&
- Logical '$ !also called logical negation#
4everse the meaning o a conditionE"ample: i !!points D 12##
means i points not bigger than 12&
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 15/18
Principles of Programming - NI July2005 15
Con&itiona" Operator
$he conditional operator !:# is used tosimpliy an ielse statement&
3ynta":
Condition E"pression/ : E"pression%$he statement above is equivalent to:
if (Condition)
Expression1
else
Expression2
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 16/18
Principles of Programming - NI July2005 16
Con&itiona" Operator cont
E"ample /:
ielse statement:
i !total D 72#
grade * J(Kelse
grade * JK5
conditional statement:
total D 72 grade * J(K: grade * JK5 '4
grade * total D 72 J(K: JK5
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 17/18
Principles of Programming - NI July2005 17
Con&itiona" Operator cont
E"ample %:
ielse statement:
i !total D 72#print!(assedMnN#5
else
print!ailedMnN#5
Conditional 3tatement:
print!;sMnN, total D 72 (assedN: ailedN#5
7/18/2019 Lecture4 Basic C Operators
http://slidepdf.com/reader/full/lecture4-basic-c-operators 18/18
Principles of Programming - NI July2005 18
SU11AR2
$his chapter e"posed you the operators usedin C
Arithmetic operators
Assignment operators
Equalities and relational operatorsLogical operators
Conditional operator
(recedence levels come into play when there
is a mi"ed o arithmetic operators in onestatement&
(repost i" - eects the result o statement
top related