Unformatted text preview:

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• FormingConditions• if/else Statements• ComparingFloating‐PointNumbers• ComparingObjects– Theequals Method– String ComparisonMethods• TheConditionalOperator(?:)• Theswitch Statemen tCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-3Flow of Control• Sequential– Executeinstructionsinorder• Methodcalls– Transfercontroltomethod,executeinstructionsinmethod,thenreturnwithorwithoutavalue• Selection– Executedifferentinstructionsdependingondata• Looping– RepeatasetofinstructionsfordifferentdataCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-4Equality Operators• Usedtodetermineifvaluesoftwoexpressionsareequalornotequal• Resultistrue orfalseisnotequaltobinary!=isequaltobinary==MeaningType(numberofoperands)EqualityoperatorsCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-5Examples• Ifint variableage holdsthevalue 32:( age == 32 ) evaluatestotrue( age != 32 ) evaluatestofalseUsetheequalityoperatorsonlywithprimitivetypesandobjectreferences,nottocompareobjectdata!Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-6• Donotconfusetheequalityoperator(==)withtheassignmentoperator(=).2Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-7Relational Operators• Usedtocomparethevaluesoftwoexpressions• Resultistrueorfalseisgreaterthanorequaltobinary>=isgreaterthanbinary>islessthanorequaltobinary<=islessthanbinary<MeaningType(numberofoperands)RelationalOperatorsCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-8Example• Ifint variableage holdsvalue 32:( age < 32 ) evaluatesto( age <= 32 ) evaluatesto( age > 32 ) evaluatesto( age >= 32 ) evaluatestofalsefalsetruetrueCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-9Logical OperatorsORBinary||ANDBinary&&NOTUnary!MeaningType(numberofoperands)LogicalOperatorOperands must be boolean expressions!Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-10Logical Operators• TheNOToperator(!)invertsthevalueofitsoperand.Iftheoperandis true,theresultwillbefalse;andiftheoperandisfalse,theresultwillbetrue.• TheANDoperator(&&)takestwobooleanexpressionsasoperands;ifbothoperandsaretrue, theresultwillbetrue,otherwiseitwillbefalse.• TheORoperator(||)takestwoboolean expressionsasoperands.Ifbothoperandsarefalse,theresultwillbefalse;otherwiseitwillbetrue.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 *ThinkoftheOR(||)operatorasa+,withtrue=1andfalse=0.ThinkoftheAND(&&)operatorasa*,withtrue=1andfalse=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-Wesleyintx,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(AANDB)=(NOTA)OR(NOTB)2. NOT(AORB)=(NOTA)AND(NOTB)• Thustofindanequivalentexpression:– change&& to||– change|| to &&– negateeachoperandexpressionCopyright © 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-18ExamplesTheseexpressionsareequivalent:( age <= 18 || age >= 65 )!( age > 18 && age < 65 )!( age > 18 ) || !( age < 65 )4Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-19Test Yourself• Arethefollowingexpressionsequivalent?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-Wesleyintage;boolisSenior,hasFever;floattemperature;age=20;temperature=102.0;isSenior=(age>= 55); //isSeniorisfalsehasFever=(temperature>98.6);//hasFeveristrueEXPRESSION VALUEisSenior&&hasFeverisSenior||hasFever!isSenior!hasFeverfalsetruetruefalseCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyWhat is the value?intage,height;age=25;height=70;EXPRESSION VALUE!(age<10)!(height>60)truefalseCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley5-22Supposewehavethree intsx,y, andz,


View Full Document

SPSU CS 1301 - Conditionals and Loops

Download Conditionals and Loops
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Conditionals and Loops and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Conditionals and Loops 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?