Vous êtes sur la page 1sur 22

DOWHILEControlStructure

ToolsforStructuredand
ObjectOrientedDesign

Objectives
Indentify,anduseinprogramdesign,counters,
accumulators,andprogramloops
Design,andidentifythecharacteristicsof,a
simplecountercontrolledprogramloop.
Identify,anduseinprogramdesign,the
DOWHILEcontrolstructure.
Identify,anduseinprogramdesign,the
preparationprogramflowchartingsymbol

Objectives(continued)
Designprogramsusingcountercontrolledloops
Designprogramsusingheaderrecordlogic
Definethetermproperprogram

UnderstandingtheAdvantagesofLooping
Whenyouusealoop,thestructurethatrepeatsactions
whilesomeconditioncontinues,withinacomputer
program,
youcanwriteonesetofinstructionsthatoperateson
multiple,separatesetsofdata
Forexample,theadvantageofhavingacomputerperform
payrollcalculationsisthatallofthedeductioninstructions
needtobewrittenonlyonceandcanberepeatedoverand
overagainforeachpaycheck

UsingaWhileLoopwithaLoopControl
Variable
Youlearnedthatalmosteveryprogramhasamain
loop,orabasicsetofinstructionsthatisrepeated
foreveryrecord
Themainloopisatypicalloopwithinit,you
writeonesetofinstructionsthatexecutes
repeatedlywhilerecordscontinuetobereadfrom
aninputfile

UsingaWhileLoopwithaLoopControl
Variable(continued)
Inadditiontothismainloop,loopsalsoappear
withinsubroutines
Usedanytimeyouneedtoperformatask
severaltimesanddontwanttowriteidentical
orsimilarinstructionsoverandover

UsingaWhileLoopwithaLoopControl
Variable(continued)

UsingaWhileLoopwithaLoopControl
Variable(continued)

Threestepsthatmustoccurineveryloopareasfollows:
1. Youmustinitializeavariablethatwillcontrolthe
loop.Thevariableinfigure63isnamedrep.
2. Youmustcomparethevariabletosomevaluethat
controlswhethertheloopcontinuesorstops.Inthis
case,youcomparereptothevalue5.
3. Withintheloop,youmustalterthevariablethat
controlstheloop.Thus,youalterrepbyadding1to
it.

UsingaWhileLoopwithaLoopControl
Variable(continued)
Oneachpassthroughtheloop,thevalueintherep
variabledetermineswhethertheloopwillcontinue
Therefore,variableslikerepareknownasloopcontrol
variables
Tostopaloop,youcomparetheloopcontrolvaluetoa
sentinelvalue
Thestatementsthatexecutewithinaloopareknownasthe
loopbody

UsingaCountertoControlLooping

UsingaCountertoControlLooping
(continued)
Acounterisanynumericvariableyouusetocountthe
numberoftimesaneventhasoccurred;infigure65,you
needacountertokeeptrackofhowmanylabelshavebeen
printedatanypoint
Eachtimeyoureadanemployeerecord,thecounter
variableissetto0
Theneverytimealabelisprinted,youadd1tothecounter
Adding1toavariableiscalledincrementingthevariable

UsingaCountertoControlLooping
(continued)

LoopingwithaVariableSentinelValue
Sometimesyoudontwanttobeforcedtorepeatevery
passthroughaloopthesamenumberoftimes
Forexample,insteadofprinting100labelsforeach
employee,youmightwanttovarythenumberoflabels
basedonhowmanyitemsaworkeractuallyproduces
Towriteaprogramthatproducesanappropriatenumber
oflabelsforeachemployee,youcanmakesomeminor
modificationstotheoriginallabelmakingprogram

LoopingwithaVariableSentinelValue
(continued)
Forexample,theinputfilevariableshavechanged;you
mustdeclareavariableforaninLastProduction
field
Additionally,youmightwanttocreateanumericfield
namedlabelsToPrintthatcanholdavalueequalto
110%ofaworkersinLastProduction
Themajormodificationtotheoriginallabelmaking
programisinthequestionthatcontrolsthelabelproducing
loop

LoopingwithaVariableSentinelValue
(continued)
InsteadofaskingiflabelCounter < 100,
younowcanaskiflabelCounter <
labelsToPrint
Thesentinelorlimitvaluecanbeavariablelike
labelsToPrintjustaseasilyasitcanbea
constantlike100

FlowchartandPseudocodeforLabel
MakingmainLoop()

LoopingbyDecrementing
Ratherthanincrementingaloopcontrolvariable
untilitpassessomesentinelvalue,sometimesitis
moreconvenienttoreducealoopcontrolvariable
oneverycyclethroughaloop
Decreasingavariablebyoneiscalled
decrementingthevariable

DOWHILEExample

DoWhileExample

DoWhileExample

DOWHILEPseudocode

DOWHILEExample

Vous aimerez peut-être aussi