Vous êtes sur la page 1sur 3

CS 1010

3/21/16

Lecture 15: Repetition Control Structure


Review
What purpose does selection control provide? Allows us to determine our action based on some kind
of condition (decision capability)
What is the structure of simple selection? IF THEN, ENDIF; IF THEN, ELSE, ENDIF
What is the structure of extended selection? IF ELSE IF (X #), THEN, ENDIF
What is the structure of nested selection? Look at notes and powerpoint.
How is relational operator used? =, <=, <> (not equal to), etc. Used as part of a condition that
evaluates to either true or false
How is a logical operator used? AND, OR, NOT (relational conditions), connect relational expressions
How many statements can a clause contain? Infinite, but must have at least 1.
What is a Boolean expression? Relational expression (true or false)
Is duplicate code in separate clauses of an extended selection control acceptable? Duplicate code is
never acceptable.
When is a compound expression linked by AND true? False? When linked by OR?
Why indent statements of a selection control?
Lecture
TheRepetitioncontrolstructureprovidescomputersolutionsthecapabilitytorepeatagroupofstatements.Forexamplean
oversimplifiedsolutiontosimulatedrivingacarmightrequirerepetitionoftheseactions:
o IFtheroadaheadisdangerousthenstop.
o IFtheroadaheadisnotsafethenapplybrakes
o IFspacetocarleftisnotsafeandspacetocarrightissafethensteerrightelseapplybrakes
o IFspacetocarrightisnotsafeandspacetocarleftissafethensteerleftelseapplybrakesIfspacebehindcarisnotsafe
thenapplybrakes(kidding)
o IFspeedtoofastthendecreasegaspedalpressure
o ELSEIFspeedistooslowthenincreasegaspedalpressure
Thesimulationwouldrepeatthesestatementsuntilthedriverstopsthecar.Repetitioncontrolprovidesthisrepeatcapability
tothecomputersolution.
Therearethreerepetitioncontrolstructures.
DOWHILE
ENDDO

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.

Vous aimerez peut-être aussi