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 

Lecture4 Basic C Operators

Embed Size (px)

DESCRIPTION

C programming

Citation preview

Page 1: Lecture4 Basic C Operators

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 

Page 2: Lecture4 Basic C Operators

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#

Page 3: Lecture4 Basic C Operators

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&

Page 4: Lecture4 Basic C Operators

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-/

Page 5: Lecture4 Basic C Operators

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

Page 6: Lecture4 Basic C Operators

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

Page 7: Lecture4 Basic C Operators

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

Page 8: Lecture4 Basic C Operators

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&

Page 9: Lecture4 Basic C Operators

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 *

Page 10: Lecture4 Basic C Operators

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

Page 11: Lecture4 Basic C Operators

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&

Page 12: Lecture4 Basic C Operators

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

Page 13: Lecture4 Basic C Operators

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  ** /&

Page 14: Lecture4 Basic C Operators

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&

Page 15: Lecture4 Basic C Operators

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

Page 16: Lecture4 Basic C Operators

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

Page 17: Lecture4 Basic C Operators

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

Page 18: Lecture4 Basic C Operators

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