Académique Documents
Professionnel Documents
Culture Documents
3/21/16
DO
ENDDO
REPEAT
UNTIL
Allthreerepetitioncontrols(alsoreferredtoasloops)haveacondition(justliketheselectioncontrolhasacondition)which
isarelationaltest,todetermineifthestatementsinsidethecontrolshouldexecuteagain.TheDOWHILEENDDOandDO
ENDDOrepetitioncontrolsdothisconditiontestatthetopoftheloop,beforeanystatementscontainedwithintheloop
execute.TheREPEATUNTILstructuretestsatthebottomoftheloop,afterallstatementscontainedintheloop.
DOWHILEconditionistrue
Statement
Statement
Statement
ENDDO
o Anypseudocodestatementmaybeplacedinsidethebodyoftheloop(wherethewordStatementisrepeatedinthe
exampleabove).
Statementexecutiondropsintothetopoftheloop(theDOWHILEconditionistrue)fromwhateverstatementisabovethe
loop.Theconditionisarelationalexpression,usingarelationaloperator:
o Continue=yesCount<100
Iftheconditionistrue,theloopbodystatementsexecuteuntiltheENDDOisreachedthenexecutionloopsbackuptothe
DOWHILE.
TheonlywaytoexitaDOWHILEENDDOloopistheloopconditionvariablemustcontainavaluethatmakestheloop
conditionfalse.Therefore,theloopmustcontainastatementthatmodifiestheloopconditionvariable.
o Endlessloopsdonothaveafalsecondition.
TheDOENDOcontrolisalsoknownasacountedloopwiththeconditiontocontinuethelooplocatedatthetopoftheloop.
start=1,limit=10
DOcount=starttolimit
Statement
Statement
ENDDO
Ifcount<=limitthestatementsrepeat.
Thecountwillincreaseandthengotofalsewhenstart=11.
Thecountfunctionallowsustorememberwhereweare(place).
LiketheDOWHILEENDDOloop,theDOENDDOloopisenteredfromthetopafterthestatementabovetheDOexecutes.
Ifthevalueinvariablecountislessthanorequaltothevalueinvariablelimit,thestatementsintheloopexecute,else
statementexecutionjumpstowhateverpseudocodestatementfollowstheENDDOstatement.ThevariablenamesintheDO
statementdonothalftobecountandlimit,thesoftwaredeveloperisfreetodetermineanynameforthesevariablesthat
reflectspurpose.
ThevariableintheDOstatementisthecountervariable,whichmaintainsthecountofhowmanytimestheloopalready
executed.Thefactthatthisvariableisnamedcounthasnothingtodowiththefactthatthisvariableistheloopcounter
variable.ThefactthatthisvariableisthevariablenexttothewordDOmakesthisvariabletheloopcountervariable,counting
thenumberoftimestheloophasexecuted.Thoughterriblestyle,thisvariablecouldbenamedxyz.
DOcount=1to10countistheloopcountervariablecountingthenumberoftimestheloopexecuted.Afterexecutionofthe
statementabovetheDOstatementtheDOloopisentered,andtheloopcountervariable(count)isinitializedtoitsstartvalue,
thevalue(orthevalueinavariable)totherightofthe=,inthiscasecountstartsat1.EachtimeENDDOexecutes,thevalue
incountincrementsby1,andstatementexecutionjumpstothetopoftheloop,theDOstatement.
o Whenthevalueincountexceedsthelimitvalue(10inthiscase)theloopcompleteditsnumberofrepetitionsand
statementexecutionproceedswithwhateverstatementfollowstheENDDOstatement.
ThestatementabovetheDOexecutes,thendropsintotheDOstatement.
SetmentalState=Happy
DOcount=1to100
DISPLAYmentalState
ENDDO
Somestatement
Happyisdisplayedonceeachtimetheloopiterates.Theloopiterates100times.Happyisdisplayed100times.
BoththeDOWHILEandtheDOloopsarepreconditionloops,meaningthetesttoseeiftheloopshouldexecutearepreloop.
ThedifferencebetweentheloopsistheDOENDDOcontrolispreferredwhenthenumberoftimesasetofinstructionsare
supposedtorepeatisknownATTHETIMETHESOLUTIONISWRITTEN.TheDOWHILEcontrolisusedwhenthe
numberofrepetitionsisnotknown,andrequiresasentinelvaluetoexittheloop.
o Sentinel=lookout
ADOWHILEloopshouldalwaysbeprecededbyapriminginputforthesentinelvalue.Thelaststatementinsidethe
DOWHILEloopshouldbethepriminginputrepeated.
PromptPleaseenteracolor,xtoexit
Getcolor
DOWHILEcolor<>x
Statements
PromptPleaseenteracolor,xtoexit
Getcolor
ENDDO
Intheprecedingslidexisthesentinelvalue,thespecialvaluethatallowsbreakingoutoftheDOWHILEloopbecausethe
conditiongoesfalse.ThepriminginputisaninputstatementabovetheDOWHILEwhichpermitstheusertoenterthesentinel
valuebeforedroppingintotheDOWHILEloopevenonce(whichpreventstheloopfromexecutingatall).Therepeated
priminginputattheendoftheDOWHILEloopspermitstheusertoexittheloopwhenallinformationisentered.
SampleDOENDDOloops
o DOcount=0to10executes11times
o DOblahblah=0to10executes11times
o DOindex=10to20executes11times
o DOindex=0to9executes10times
o DOindex=1to10executes10times
SampleDOWHILEENDDOconditions
o DOWHILEcontinue=yessentinelvalueyes
o DOWHILEcolor<>redsentinelvaluered
PromptDoyouwishtocontinue,yesorno?
Getcontinue
DOWHILEcontinue=yes
Statement
Statement
PromptDoyouwishtocontinueyesorno?
Getcontinue
ENDDO
PromptPleaseguessmyfavoritecolor
GetfavColor
DOWHILEfavColor<>orange
PromptPleaseguessmyfavoritecolor
GetfavColor
ENDDO
DisplayYouguessedmyfavoritecolor!
count=1
PromptTwotriestoguessmyfavoritecolor,guess?
GetfavColor
DOWHILEfavColor<>orangeANDcount<2
count=count+1
PromptTwotriestoguessmyfavoritecolor,guess
GetfavColor
ENDDO
IFfavColor=orangeTHEN
DisplayYouguessedmyfavoritecolor!
ENDIF
ThethirdrepetitioncontrolisREPEATUNTIL.Thiscontrolisaposttestloopmeaningtheloopexecutesoncethenteststo
seeifthestatementsinsidetheloopshouldrepeat(locatedatbottom,whilepretestislocatedattop.Thisloopstructure
guaranteesatleastoneexecutionoftheloop.Forthosewithprogrammingexperience,thisloopiscomparabletoadoloop.
ExamplesinLecture.