You are on page 1of 11

COMPARING VALUES C Relational Operators Relational Operator == > < >= <= !

= Description equal to greater than less than greater than or eqaul to less than or equal to not equal to

x = -5 power = 1024 !"#$O% = 1024 &=' ite( = 1)5 *+#*,- = -...)0 x <= 0 power < !"#$O% x >= & ite( > *+#*,x less than or equal to 0 power less than !"#$O% x greater than or equal to & ite( greater than *+#*,1 /true0 0/1alse0 0/1alse0 1 /true0

Precedence and Associativity o Relational Operators Operators !ssociati2it& / 0 33 /post1ix0 -- /post1ix0 le1t to right 3 /unar&0 - /unar&0 33 /pre1ix0 -- /pre1ix0 right to le1t 4 5 6 le1t to right 3 le1t to right ! " !# "# le t to rig$t = 3= -= 4= 5= etc) right to le1t
Page 1

E%a&ple' int i = 57 int 8 = 107 int 9 = 157 int l = 57 i==l 8<9 9>i 8 != 9 i>8 9<8 8==l I State&ent $er1or(s :ecision or :ata testing an: execute certain state(ent ;ase: on the results o1 those :ecisions) Format 1: *1 /con:ition0 < ;loc9 o1 one or (ore = state(ent7> Example: *1 /age < 1?0 < print1/@Aou cannot 2ote &etBnC07 &rs = 1? D age7 54 calculates how (an& &ears until the user can 2ote 45 print1/@Aou can 2ote in 6: &ears)BnCE &rs07> Format 2: *1 /con:ition0 < ;loc9 o1 one or (ore = state(ent7> else < ;loc9 o1 one or (ore = state(ent7>
Page (

Example: *1 /age < 1?0 < print1/@Aou cannot 2ote &etBnC07 &rs = 1? D age7 54 calculates how (an& &ears until the user can 2ote 45 print1/@Aou can 2ote in 6: &ears)BnCE &rs07> else < print1/@AouF2e (a:e it to a:ulhoo:)Bn07> Example What do these statements display? 2) 'ar1 = 25.12; 1) if (12 < 12) 'ar2 = 15.00; printf(less); if ('ar1 <= 'ar2) else printf(less or e()al); printf(not less); else printf(*reater than); O)tp)t' * O)tp)t' * What value is assigned to x when y is 15.0? 2) if (y<15.0) 1) x = 25.0 if (y#=0.0) if (y =(x!10.0)) x = 5 $ y; x = x ! 10.0; else else x = 2 $ y; x = x " 2.0; else x = % $ y; after executing, x is ? %) if (y<15.0 && y#=0.0) x = 5 $ y; else x = 2 $ y; after executing, x is ?
Page +

after executing, x is ?

Practice %rite = state(ents to carr& out the 1ollowing steps) 1) *1 x is 0E a:: 1 to Gero#count) *1 x is negati2eE a:: x to (inus#su() 2) i1 su( is equal to 10 an: total is less than 20E print the text string Hincorrect)H) I) i1 1lag is 1 or letter is not an J"JE then assign the 2alue 0 to exit#1lagE else set exit#1lag to 1) if statements with compound statements +f there is a need to exe,)te se'eral statements (compound statement)- then left and ri*ht .ra,es ,an *ro)p these statements. When the sym.ol / follo0s the ,ondition or else- the 1 ,ompiler either exe,)tes or s2ips all statements thro)*h the mat,hin* 3. if statement formats: if statement format: if (condition ! statementt1" statementt#" statementtn" $ If else statement format: if (condition ! statementt1" statementtn" $ else! statementf1" statementfn" $

Example 1) if (pop4today # pop4yesterday) / *ro0th = pop4today ! pop4yesterday; *ro0th4p,t = 100.0 $ *ro0th " pop4yesterday; printf(5he *ro0th per,enta*e is 6 .2f7n- *ro0th4p,t); 3

Page ,

2) if (,tri <= 89:4;9<=415>+)/ printf(1ar ?6d@ safe7n- a)to4id); safe = safe A 1; 3 else / printf(1ar ?6d@ )nsafe7n- a)to4id); )nsafe = )nsafe A 1; 3 %ractice >e'ise the style of the follo0in* if statement to impro'e its reada.ility. if (en*ine4type == BCD / printf(Cet en*ine); speed4,ate*ory = 1;3 else / printf(Eropellers); speed4,ate*ory = 2;3 Write an intera,ti'e pro*ram that ,ontains an if statement that may .e )sed to ,omp)te the area of s()are ( area = side $ side) or a trian*le ( area = 1"2 $ .ase $ hei*ht) after promptin* the )ser to type the first ,hara,ter of the fi*)re name (; or 5). Nested i state&ents and M)ltiple-Alternative .ecisions +este: i1 state(ent - an i1 state(ent with another i1 state(ent as its true tas9 or its 1alse tas9) Example 1: if (x # 0) n)m4pos = n)m4pos A 1; else if (x < 0) n)m4ne* = n)m4ne* A 1; else n)m4Fero = n)m4Fero A 1; Series o i s' i1 /x>00 nu(#pos = nu(#pos 3 17 i1 /x<00 nu(#neg = nu(#neg 3 17 i1 /x==00 nu(#Gero = nu(#Gero 3 17
Page /

Example 2: if (x # 5) if (x < 10) printf (x is .et0een 5 and 10.); if (x # 5 && x < 10) printf (x is .et0een 5 and 10.);

&ested if'else or (ultiple')lternative *ecision +orm Example Write a 1 pro*ram that prompts a )ser to enter the final *rade of a s).Ge,t. 5he pro*ram 0ill then o)tp)t the e()i'alent *rade a,,ordin* to the follo0in* ta.le@ ,rade 00.00!HI.JI HI.50!LJ.JI LJ.50!LI.JI LI.50!KJ.JI -.uivalent 0.0 1.0 1.5 2.0 ,rade -.uivalent KJ.50!KK.JI 2.5 KK.50!I2.JI %.0 I2.50!IH.JI %.5 IH.50!100.00 J.0

Solution if (final4*rade <= HI.JI) e()i'alent = 0.5; else if (final4*rade <= LJ.JI) e()i'alent = 1.0; else ... if (final4*rade <= IH.JI) e()i'alent = %.5; else e()i'alent = J.0; ...

Page 0

Precedence and Associativity o E1)ality Operators Operators !ssociati2it& / 0 33 /post1ix0 -- /post1ix0 le1t to right 3 /unar&0 - /unar&0 33 /pre1ix0 -- /pre1ix0 right to le1t 4 5 6 le1t to right 3 le1t to right < > <= >= le1t to right = = != le1t to right = 3= -= 4= 5= etc) right to le1t *angling else 1onsider the follo0in* ,ode@ if (x = = 2) if (y = = %) print (>ose7n); else print (8arie7n); if (x = = 2) if (y = = %) print (>ose7n); else print (8arie7n);

&ote: 5he r)le is that an else atta,hes itself to the nearest if. E%ercise 1' -nter a user salar&) *1 the user earne: (ore than 100E000E a congratulator& (essage is printe:) *1 the user earne: less than 100E000 a (essage prints telling the user how 1ar awa& 1ro( 100E000 he or she is) Logical Operators =o(;ining (ore than one con:ition ,hese allow the testing o1 (ore than one con:ition as part o1 selection state(ents) Kogical expressions in2ol2e Loolean operations) && MM and or not

Page 2

exp1 0 0 1 1

exp2 0 1 0 1

!exp1 1 1 0 0

!exp2 1 0 1 0

exp1MMexp2 exp1NNexp2 0 0 0 1 0 1 1 1

Precedence and Associativity of Logical Operators Operators !ssociati2it& / 0 33 /post1ix0 -- /post1ix0 le1t to right 3 /unar&0 - /unar&0 33 /pre1ix0 -- /pre1ix0 ! right to le1t 4 5 6 le1t to right 3 le1t to right < > <= >= le1t to right == != le1t to right 33 le t to rig$t 44 le t to rig$t = 3= -= 4= 5= etc) right to le1t Examples !ssu(e that x = OE & = 2E G = 20 x < = G MM x < & 1alse & = = G NN x ! = G true x 3 15 > G MM !/& > G0 true !/x > &0 NN & > G - 10 1alse x > & NN x = = G MM & < G true !/x < G0 MM x >= & 3 4 NN G = = 1. 1alse Example: *1 /age >= 210 < *1 /age <= O50 < $rint1/@Aou are within range)BnC07 > >

Page 5

i1 //age >= 210 MM /age <= O500 < print1/@Aou are within range)BnC07 if (marital4stat)s == B;D) if (*ender == B8D) if (a*e #= 1K && a*e <=2H) printf(9ll ,riteria are met.7n); or if (marital4stat)s == B;D && *ender == B8D && a*e #= 1K && a*e <=2H) printf(9ll ,riteria are met.7n); s6itc$ State&ent ,he switch state(ent is a (ulti-wa& con:itional state(ent generaliGing the if-else state(ent) *t is use: to select one o1 se2eral alternati2es) *t especiall& use1ul when the selection is ;ase: on the 2alue o1 a single 2aria;le or o1 a si(ple expressions/calle: the controlling expression0) ,he 2alue o1 this expression (a& ;e o1 t&pe int or charE ;ut not o1 t&pe :ou;le) Format: switch /variable0 < case constant1P state(ent sequence 1 ;rea97 case constant2P state(ent sequence 2 ;rea97 case constantIP state(ent sequence I ;rea97 :e1aultP :e1ault state(ent sequence >
Page 7

Example *(ple(ent the 1ollowing using switch state(ent =lass *D Qhip =lass L or ; Lattleship = or c =ruiser D or : Destro&er R or 1 Rrigate Solution char class7 print1/@-nter class>C07 scan1/@6cCE Mclass07 switch/class0< case SLFP case S;FP print1/@LattleshipBnC07 ;rea97 case S=FP case ScFP print1/@=ruiserBnC07 ;rea97 case SDFP case S:FP print1/@Destro&erBnC07 ;rea97 Practice: %rite a switch state(ent that assigns to the 2aria;le lu(ens the expecte: ;rightness o1 a stan:ar: light ;ul; whose wattage has ;een store: in watts) Tse this ta;leP %atts Lrightness /in lu(ens0 15 125 25 215 40 500 O0 ??0 '5 1000

100 1O'5 4!ssign -1 to lu(ens i1 the 2alue o1 watts is not in the ta;le)
Page 18

/onditional 0perator ;treamlines some lo*i, and is strai*htfor0ard to )se. <ormat@ relation ? trueStatement : falseStatement; =xample@ *1 /total <= I?50)00 < total 4= 1)107 > else < total 4= 1)057 > / total <= I?50)0 0 U / total 4= 1)107 0 P / total 4= 1)05 07 Exercises:
%. 9 *lass ret)rn ,ompany re()ires a pro*ram 0hi,h 0ill ,al,)late the amo)nt of ,redit d)e to a ,)stomers 0ho ret)rns a ,ase- or ,ases of empty .ottles. (Nne ,ase ,ontains 10 .ottles.) +np)t to the pro*ram is a re,ord- ,ontainin* the ,)stomerDs name and a n)m.er. 5his n)m.er ,ontains the n)m.er of f)ll or partly f)ll ,ases of empty .ottles- 0hi,h the ,)stomer has ret)rned (e.*. K.5 indi,ates K f)ll ,ases and one half!f)ll ,ase.) +f a ,ase ret)rned .y a ,)stomer is more than half f)ll- it is to .e ,o)nted as f)ll. +f K or more ,ases are ret)rned- the ,)stomer is to re,ei'e E5.00 per ,ase- other0ise the ,)stomer re,ei'es E%.50 per ,ase. 5he pro*ram is to print the ,)stomerDs name- the n)m.er of ,ases ret)rned- the n)m.er of f)ll ,ases ,redited and the ,redit amo)nt d)e. J. =nter a personDs a*e and display remar2 .ased on the ta.le@ 9*e >emar2 12 .elo0 1hild 1% O 1I 5eens 20 O 5I 9d)lt H0 a.o'e ;enior 1itiFen
Page 11

You might also like