Professional Documents
Culture Documents
ToolsforStructuredand
ObjectOrientedDesign
Objectives
Identify,anduseinprogramdesign,the
IFTHENELSEcontrolstructure
Identify,anduseinprogramdesign,thedecision
andconnectorprogramflowchartingsymbols
Usepseudocodeasatoolinprogramdesign
Identify,anduseinprogramdesign,thenull
ELSEpattern
Objectives(continued)
Identify,anduseinprogramdesign,the
sequentialIFTHENELSEpattern
Identify,anduseinprogramdesign,thenest
IFTHENELSEpattern
Distinguishbetweenthelogicrepresentedbya
sequentialIFTHENELSEpatternandthelogic
representedbyanestedIFTHENELSEpattern
UnderstandingtheIF/THEN/ELSE
Structures
Thesecondstructureiscalledaselection
structureordecisionstructure
Youaskaquestion
dependingontheanswer,
youtakeoneoftwocoursesofaction
nomatterwhichpathyoufollow,youcontinue
withthenextevent
UnderstandingtheIF/THEN/ELSE
Structures(continued)
UnderstandingtheIF/THEN/ELSE
Structures(continued)
Theselectionstructureissometimescalledanif
thenelsebecauseitfitsthefollowingstatement:
ifsomeConditionistruethen
dooneProcess
else
dotheOtherProcess
UnderstandingtheIF/THEN/ELSE
Structures(continued)
EvaluatingBooleanExpressionstoMake
Comparisons
Theselectionstructure(sometimescalledadecision
structure)isoneofthebasicstructuresofstructured
programming
IF/THEN/ELSEFlowchart
EvaluatingBooleanExpressionstoMake
Comparisons(continued)
Adualalternative,orbinary,selection:
hasanactionassociatedwitheachoftwopossible
outcomes
Thisselectionstructureisalsocalledanifthenelse
structurebecauseitfitsthestatement:
if the answer to the question is yes, then
do something
else
do somethingElse
endif
EvaluatingBooleanExpressionstoMake
Comparisons(continued)
Asinglealternative,orunary,selection
actionisrequiredforonlyoneoutcomeofthequestion
ABooleanexpressionisonethatrepresentsonlyoneof
twostates,usuallyexpressedastrueorfalse
Everydecisionyoumakeinacomputerprograminvolves
evaluatingaBooleanexpression
UsingtheLogicalComparisonOperators
Usually,youcancompareonlyvaluesthatareof
thesametype:
comparenumericvaluestoothernumeric
values
comparecharactervaluestoothercharacters
Youcanaskeveryprogrammingquestionby
usingoneofonlythreetypesofcomparison
operatorsinaBooleanexpression
UsingtheLogicalComparisonOperators
(continued)
Foranytwovaluesthatarethesametype,youcandecide
whether:
Thetwovaluesareequal
Thefirstvalueisgreaterthanthesecondvalue
Thefirstvalueislessthanthesecondvalue
InanyBooleanexpression,thetwovaluesusedcanbe
eithervariablesorconstants
Eachprogramminglanguagesupportsitsownsetof
logicalcomparisonoperators,orcomparisonsymbols,that
expresstheseBooleantests
UsingtheLogicalComparisonOperators
(continued)
Mostlanguagesallowyoutousethealgebraicsignsfor
greaterthan(>)andlessthan(<)tomakethe
correspondingcomparisons
Additionally,COBOL,whichisverysimilartoEnglish,
allowsyoutospelloutthecomparisonsinexpressionslike
dayPastDue is greater than 30? or
packageWeight is less than
maximumWeightAllowed?
RPGusesthetwoletterabbreviationsGTandLTto
representgreaterthanorlessthan
UsingtheLogicalComparisonOperators
(continued)
Mostprogramminglanguagesalsoprovideforthree
additionalcomparisons
Foranytwovaluesthatarethesametype,youcandecide
whether:
Thefirstisgreaterthanorequaltothesecond.
Thefirstislessthanorequaltothesecond.
Thetwoarenotequal.
Anylogicalsituationcanbeexpressedusingjustthreetypes
ofcomparisons:equal,greaterthan,andlessthan
UsingtheLogicalComparisonOperators
(continued)
Comparingtwoamountstodecideiftheyarenot
equaltoeachotheristhemostconfusingofallthe
comparisons
Usingnotequaltoindecisionsinvolves
thinkingindoublenegatives,whichmakesyou
pronetoincludelogicalerrorsinyourprogram
UsingtheLogicalComparisonOperators
(continued)
IF/THEN/ELSEFlowchart
IFTHENELSEGeneric
UsingtheLogicalComparisonOperators
(continued)
Besidesbeingawkwardtouse,thenotequalto
comparisonoperatoristheonemostlikelytobe
differentinvariousprogramminglanguages
COBOLallowsyoutowritenotequalto
Pascalusesalessthansignfollowed
immediatelybyagreaterthansign(<>)
C#,C++,CandJavauseanexclamationpoint
followedbyanequalsign(!=)
UnderstandingANDLogic
Often,youneedmorethanoneselectionstructureto
determinewhetheranactionshouldtakeplace
Forexample,supposethatyouremployerwantsareport
thatlistsworkerswhohaveregisteredforbothinsurance
plansofferedbythecompany:themedicalplanandthe
dentalplan
KnownasanANDdecisionbecausetheemployees
recordmustpasstwotestsparticipationinthemedical
planandparticipationinthedentalplanbeforeyouwrite
thatemployeesinformationonthereport
UnderstandingANDLogic(continued)
Acompound,or
AND,decision
requiresanested
decision,ora
nestedifthatis,
adecisioninside
ofanother
decision
WritingNestedANDDecisionsfor
Efficiency
Whenyounestdecisionsbecausetheresultingaction
requiresthattwoconditionsbetrue,youmustdecide
whichofthetwodecisionstomakefirst
Logically,eitherselectioninanANDdecisioncancome
first
However,whentherearetwoselections,youoftencan
improveyourprogramsperformancebymakingan
appropriatechoiceastowhichselectiontomakefirst
WritingNestedANDDecisionsfor
Efficiency(continued)
InmanyANDdecisions,youhavenoideawhichoftwo
eventsismorelikelytooccur;
Inthatcase,youcanlegitimatelyaskeitherquestion
first
Inaddition,eventhoughyouknowtheprobabilityofeach
oftwoconditions,thetwoeventsmightnotbemutually
exclusive
Inotherwords,onemightdependontheother
CombiningDecisionsinanAND
Selection
Mostprogramminglanguagesallowyoutoasktwoor
morequestionsinasinglecomparisonbyusingalogical
ANDoperator
IftheprogramminglanguageyouuseallowsanAND
operator,youstillmustrealizethat
thequestionyouplacefirstisthequestionthatwillbe
askedfirst
casesthatareeliminatedbasedonthefirstquestionwill
notproceedtothesecondquestion
CombiningDecisionsinanAND
Selection(continued)
Thecomputercanaskonlyonequestionatatime
CombiningDecisionsinanAND
Selection(continued)
UnderstandingORLogic
Sometimes,youwanttotakeactionwhenoneortheother
oftwoconditionsistrue
CalledanORdecisionbecause
eitheroneconditionmustbemetor
someotherconditionmustbemet,inorderforanevent
totakeplace
Ifsomeoneasksyou,AreyoufreeFridayorSaturday?,
onlyoneofthetwoconditionshastobetrueinorderfor
theanswertothewholequestiontobeyes
onlyiftheanswerstobothhalvesofthequestionare
falseisthevalueoftheentireexpressionfalse
UnderstandingORLogic(continued)
IncorrectFlowchartformainLoop()
Thisflowchartisnotallowedbecauseitisnotstructured
WritingORDecisionsforEfficiency
Youcanwriteaprogramthatcreatesareport
containingallemployeeswhohaveeitherthe
medicalordentalinsurancebyusingthe
mainLoop()
WritingORDecisionsforEfficiency
(continued)
WritingORDecisionsforEfficiency
(continued)
WritingORDecisionsforEfficiency
(continued)
Oneoftheseselectionsissuperiortotheother
Usingeitherscenario,950employeerecordsappearonthe
list,butthelogicusedinFigure526requires1,100
decisions,whereasthelogicusedinFigure527requires
1,500decisions
Thegeneralruleis:InanORdecision,firstaskthe
questionthatismorelikelytobetrue
CombiningDecisionsinanORSelection
Ifyouneedtotakeactionwheneitheroneortheotherof
twoconditionsismet,youcanusetwoseparate,nested
selectionstructures,asinthepreviousexamples
However,mostprogramminglanguagesallowyoutoask
twoormorequestionsinasinglecomparisonbyusinga
logicalORoperator
UsingSelectionsWithinRanges
Businessprogramsoftenneedtomakeselectionsbasedon
avariablefallingwithinarangeofvalues
Whenyouusearangecheck,youcompareavariabletoa
seriesofvaluesbetweenlimits
Toperformarangecheck,makecomparisonsusingeither
thelowestorhighestvalueineachrangeofvaluesyouare
using
InefficientRangeSelectionIncluding
UnreachablePath
UnderstandingPrecedenceWhen
CombiningANDandORSelections
Mostprogramminglanguagesallowyoutocombineas
manyANDandORoperatorsinanexpressionasneeded
Thelogicbecomesmorecomplicatedwhenyoucombine
ANDandORoperatorswithinthesamestatement
WhenyoucombineANDandORoperators,theAND
operatorstakeprecedence,meaningtheirBooleanvalues
areevaluatedfirst
YoucanavoidtheconfusionofmixingANDandOR
decisionsbynestingifstatementsinsteadofusingANDs
andORs
UnderstandingtheCaseStructure
TheLogicbelowiscompletelystructured
UnderstandingtheCaseStructure
(continued)
Writingthelogicusingacasestructure,asshownin
Figure537,mightmakeiteasiertounderstand
Thecasestructureprovidesaconvenientalternativeto
usingaseriesofdecisionswhenyoumustmakechoices
basedonthevaluestoredinasinglevariable
Whenusingthecasestructure,youtestavariableagainsta
seriesofvalues,takingappropriateactionbasedonthe
variablesvalue
FlowchartandPseudocodeofHousing
ModelUsingtheCaseStructure
IFTHENELSEExamplewithnullELSE
IFTHENELSEExample
IFTHENELSEExample
NestedIFTHENELSEExample
NestedIFTHENELSEExample
IFTHENELSEPseudocodeExample
GUIIFTHENELSEExample