Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.