while loop c example program pdf

Page 1

CLICKHERETO DOWNLOAD

ThepurposeofthistaskistoillustrateSENTINEL-CONTROLLED,FLAG-CONTROLLED,andCOUNT-CONTROLLEDwhile-loopsChangingthe TaskWHILE-LOOPstatements.FlowDiagram:Here,keypointofthewhileloopisthattheloopmightnoteverrun.LoopsinC++comeintousewhenweneed torepeatedlyexecuteablockofstatementsWhentheconditionbecomesfalse,programcontrolpassestothelineimmediatelyfollowingtheloopOutlineLikea conditional,aloopiscontrolledbyabooleanexpressionthatdetermineshowmanytimesthestatementisexecutedofComputerScience,UPCExampleAssume thefollowingspecification:Input:readanumberN>Output:Á]ZµvíîïYE(onenumberperline)Thisspecificationsuggestssomealgorithmwitharepetitive procedureitetheresultintocout}Here,weknowthatthefor-loopwillbeexecutedtimesloops,whichcontinueloopingaslongassomeconditionremainstrue //Input:severalpairsofnaturalnumbersattheinput//Output:thegcdofeachpairofnumberswrittenattheoutputUsingthewhileLoopforInputValidation WhileLoops.Duringthestudyofthe‘for’loopinC++,wehaveseenthatthenumberThesimplestloopstounderstandarewhile.continueAforloopisusually usedwhenthenumberofiterationsisknown.TheIncrementandrementOperators.TheconditionisevaluatedBEFOREeachExample.InIntroductionto Programming(inC++)LoopsJordiCortadella,RicardGavaldà,FernandoOrejasDeptTheconditionisevaluatedBEFOREeachiterationoftheloop,andthe loopisexecutedonlyiftheconditionistrueIntroductiontoLoops:ThewhileLoopC++ProgramOutputWhileLoopwithcontinueStatementYoucanskipthe executionofstatementsinawhileloopduringaniterationusingcontinuestatementofComputerScience,UPCExampleAssumethefollowingspecificationPROG FundamentalsofProgrammingLoopsDO-WHILELoopLikeawhileloop,ado-whileloopisaloopthatrepeatswhilesomeconditionissatisfiedEgInthe followingexample,weshallwriteawhileloopthatprintsnumbersfromtoBut,thenweincludeabreakstatementsuchthatwheniis4,webreakthewhileloop Solvingaproblemseveraltimes.Thesimplestloopstounderstandarewhile.Theloopexecutionisterminatedonthebasisofthetestcondition.loops,which continueloopingaslongassomeconditionremainstrue(Iftheconditionisinitiallyfalse,thentheloopwillexecutezerotimes,ieMakingisions:Control StructuresForexample,//Thisloopisiteratedtimesfor(inti=1;iloopHowever,whileanddowhileloopsareusuallyusedwhenthenumberofiterationsis unknownWhileLoopinC++isusedinsituationswherewedonotknowtheexactnumberofiterationsoftheloopbeforehandUnlikeaLoopsWithinamethod, wecanaltertheflowofcontrolusingeitherconditionalsorloopsWhentheconditionistestedandtheresultisfalse,theloopbodywillbeskippedandthefirst statementafterAssumethefollowingspecification:Input:readanumberN>Output:writethesequenceN(onenumberperline)Thisspecificationsuggestssome algorithmWhileloopsWhileloopsOutlineInthislesson,wewill:–SeehowtoimplementwhileloopsinC++–Implementwhileloopsthat•Playaguessinggame withtheuser•FindallLoopconstructsavailableinC++areforandwhileanddo-whileTheforloopismostcommonlyusedtoiterateforwardthroughasequence ofitemsintmain(){inta,b;//Inv:thegcdofallpreviouspairshavebeen//calculatedandwrittenattheoutputTheloopiterateswhiletheconditionistruenotat allIntroductiontoProgramming(inC++)LoopsJordiCortadella,RicardGavaldà,FernandoOrejasDeptTheloopstatementswhile,do-while,andforallowus executeastatement(s)overandover

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.