Sei sulla pagina 1di 2

Overview

Alongwithsequentialanddecisionstructures,anothertypeofprogramming
structureisrepetition,alsocalledloops.Thestructureistypifiedbyoneormore
repeatingstatements.Therearethreetypesofloopseachofwhichissetapartby
howtheyperformtherepetition.
DoWhile

DoUntil
ForNext

anexpressionreturnseither
trueorfalse
statementsrepeatwhile
conditionistrue
repeatsuntiltestconditionis
true
Isacounterandrepeatsonly
aspecifiednumberoftimes

Countersareusefultoolstocontainloops.Bysettingthecountertocountupor
downopenparenthesesincrementordecrement),thenumberofiterationscanbe
controlledortract.Loopscanalsobecontrolledbytheuserbyallowingthemto
selectthenumberofiterationstheyneedreturned.
Loopsmustincludeameansofterminationifnotitbecomesaninfiniteloopthat
willrepeatitselfuntilacteduponexternally.Infiniteloopscancauseprogramsor
evensystemstobecomeunresponsive.InVisualStudioandVisualStudioCode,
repeatingstatementswillindentsotheycanbeeasilyspottedandcheckedforclose
conditions.
Loopscanalsobenested,simultaneouslyperformingiterations,sometimesat
differentrates.Thetextbookusesaclockanalogywiththreehandsthatmoveat
differentspeedsalongthesamepath.Eachloopisrunningandperformingatask
butmayreturnresultsoriterateatdifferenttimes.

Potrebbero piacerti anche