1Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyChapter 5Part 1 Conditionals and LoopsCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-2Topics• FormingConditions• if/else Statements• ComparingFloating‐PointNumbers• ComparingObjects– Theequals Method– String ComparisonMethods• TheConditionalOperator(?:)• Theswitch Statemen tCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-3Flow of Control• Sequential– Executeinstructionsinorder• Methodcalls– Transfercontroltomethod,executeinstructionsinmethod,thenreturnwithorwithoutavalue• Selection– Executedifferentinstructionsdependingondata• Looping– RepeatasetofinstructionsfordifferentdataCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-4Equality Operators• Usedtodetermineifvaluesoftwoexpressionsareequalornotequal• Resultistrue orfalseisnotequaltobinary!=isequaltobinary==MeaningType(numberofoperands)EqualityoperatorsCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-5Examples• Ifint variableage holdsthevalue 32:( age == 32 ) evaluatestotrue( age != 32 ) evaluatestofalseUsetheequalityoperatorsonlywithprimitivetypesandobjectreferences,nottocompareobjectdata!Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-6• Donotconfusetheequalityoperator(==)withtheassignmentoperator(=).2Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-7Relational Operators• Usedtocomparethevaluesoftwoexpressions• Resultistrueorfalseisgreaterthanorequaltobinary>=isgreaterthanbinary>islessthanorequaltobinary<=islessthanbinary<MeaningType(numberofoperands)RelationalOperatorsCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-8Example• Ifint variableage holdsvalue 32:( age < 32 ) evaluatesto( age <= 32 ) evaluatesto( age > 32 ) evaluatesto( age >= 32 ) evaluatestofalsefalsetruetrueCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-9Logical OperatorsORBinary||ANDBinary&&NOTUnary!MeaningType(numberofoperands)LogicalOperatorOperands must be boolean expressions!Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-10Logical Operators• TheNOToperator(!)invertsthevalueofitsoperand.Iftheoperandis true,theresultwillbefalse;andiftheoperandisfalse,theresultwillbetrue.• TheANDoperator(&&)takestwobooleanexpressionsasoperands;ifbothoperandsaretrue, theresultwillbetrue,otherwiseitwillbefalse.• TheORoperator(||)takestwoboolean expressionsasoperands.Ifbothoperandsarefalse,theresultwillbefalse;otherwiseitwillbetrue.Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-11Truth Tablefalsefalsetruefalsefalsetruefalsetruetruefalsetruefalsefalsefalsetruetruetruefalsetruetruea || ba && b!abaCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThink + and *ThinkoftheOR(||)operatorasa+,withtrue=1andfalse=0.ThinkoftheAND(&&)operatorasa*,withtrue=1andfalse=0.True||False 1+0=1(True)False&&True 0*1=0(False)3Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-13Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesleyintx,y;x=4;y=6;EXPRESSION VALUEx<yx+2<yx!=yx+3>=yy==xy==x+2truefalsetruetruefalsetrueCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyEXPRESSION VALUE7==713<100‐17.32!=‐17.32‐3.0==0.013<=100‐18<‐154.2>3.713<=130.012>0.013truetruefalsefalsetruetruetruetruefalseCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-16Equivalence of ExpressionsDeMorganʹsLaws:1. NOT(AANDB)=(NOTA)OR(NOTB)2. NOT(AORB)=(NOTA)AND(NOTB)• Thustofindanequivalentexpression:– change&& to||– change|| to &&– negateeachoperandexpressionCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-17Negation of Equality and Relational Operatorsa < ba >= ba <= ba > b a > ba <= ba >= ba < ba == ba != ba != ba == b!(Expression)ExpressionCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-18ExamplesTheseexpressionsareequivalent:( age <= 18 || age >= 65 )!( age > 18 && age < 65 )!( age > 18 ) || !( age < 65 )4Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-19Test Yourself• Arethefollowingexpressionsequivalent?Expr 1Expr 2! (!a) a! (a && b) !a || !b! (!a && !b) a && b! (!a && !b) a || bCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesleyintage;boolisSenior,hasFever;floattemperature;age=20;temperature=102.0;isSenior=(age>= 55); //isSeniorisfalsehasFever=(temperature>98.6);//hasFeveristrueEXPRESSION VALUEisSenior&&hasFeverisSenior||hasFever!isSenior!hasFeverfalsetruetruefalseCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyWhat is the value?intage,height;age=25;height=70;EXPRESSION VALUE!(age<10)!(height>60)truefalseCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-22Supposewehavethree intsx,y, andz,
View Full Document