C-chap05-Repetition

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity1Chapter5RepetitionChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity2ObjectivesBasicLoopStructuresThewhileStatementComputingSumsandAveragesUsingawhileLoopTheforStatementCaseStudies:LoopProgrammingTechniquesNestedLoopsThedo-whileStatementCommonProgrammingandCompilerErrorsChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity3KeypointsBasicLoopStructuresThewhileStatementTheforStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity4IntroductionAsectionofcodethatisrepeatediscalledaloop(循环),becauseafterthelaststatementinthecodeisexecuted,theprogrambranches,orloops,backtothefirststatementandstartsanotherrepetitionthroughthecodeEachrepetitionisalsocalledaniteration(迭代)orpassthroughtheloop(通过循环)Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity5BasicLoopStructuresConstructingarepeatingsectionofcoderequiresthatfourelementsbepresent:Repetitionstatement(循环语句)whilestatementforstatementdo-whilestatementCondition(条件)Astatementthatinitiallysetstheconditionbeingtested(设置被测条件式初始值的语句)Astatementwithintherepeatingsectionofcodethataltersthecondition(改变条件式的语句)sothatiteventuallybecomesfalseChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity6PretestandPosttestLoopspretestloop(前测试循环)Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity7PretestandPosttestLoopsposttestloop(后测试循环)Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity8Counter-ControlledandCondition-ControlledLoopsCounter-controlledloop(计数器控制循环):theconditionisusedtokeeptrackofthenumberofrepetitionsAlsoknownasafixed-countloop(固定计数循环)Condition-controlledloop(条件控制循环):thetestedconditiondoesnotdependonacountbeingachieved,butratheronaspecificvaluebeingencounteredChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity9BasicLoopStructuresChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity10ThewhileStatementThegeneralformofthewhilestatementiswhile(expression)statement;Thetransferofcontrolbacktothestartofawhilestatementtoreevaluatetheexpressionisknownasaprogramloop(程序循环)Thefollowingisavalidbutinfiniteloop:while(count=10)printf(%d,count);Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity11ThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity12ThewhileStatementOutputis:12345678910Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity13ThewhileStatementOutputis:10987654321Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity14ThewhileStatementOutputis:NUMBERSQUARECUBE----------------111248392741664525125636216749343864512981729101001000Chapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity15Condition-controlledloopOutputis:DEGREESDEGREESCELSIUSFAHRENHEIT-----------------541.001050.001559.002068.002577.003086.003595.0040104.0045113.0050122.00ThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity16Example-Fibonacci1,1,2,3,5,8,13,21,34,55,…Noticethatweonlyneedtwoelementsinordertocalculatethenextone.11+112+11211231123+1123511235+11235811235811235813112358131123581321ThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity17intfib1=1,fib2=1,fib_next=0;printf(%d,fib1);while(fib2lim){printf(%d,fib2);fib_next=fib1+fib2;fib1=fib2;fib2=fib_next;}printf(\n);Screen8lim1fib11fib20fib_nextThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity18intfib1=1,fib2=1,fib_next=0;printf(%d,fib1);while(fib2lim){printf(%d,fib2);fib_next=fib1+fib2;fib1=fib2;fib2=fib_next;}printf(\n);1Screen8lim1fib11fib20fib_nextThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity19intfib1=1,fib2=1,fib_next=0;printf(%d,fib1);while(fib2lim){printf(%d,fib2);fib_next=fib1+fib2;fib1=fib2;fib2=fib_next;}printf(\n);1Screen8lim1fib11fib20fib_nextThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity20intfib1=1,fib2=1,fib_next=0;printf(%d,fib1);while(fib2lim){printf(%d,fib2);fib_next=fib1+fib2;fib1=fib2;fib2=fib_next;}printf(\n);11Screen8lim1fib11fib20fib_nextThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity21intfib1=1,fib2=1,fib_next=0;printf(%d,fib1);while(fib2lim){printf(%d,fib2);fib_next=fib1+fib2;fib1=fib2;fib2=fib_next;}printf(\n);11Screen8lim1fib11fib22fib_nextThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity22intfib1=1,fib2=1,fib_next=0;printf(%d,fib1);while(fib2lim){printf(%d,fib2);fib_next=fib1+fib2;fib1=fib2;fib2=fib_next;}printf(\n);11Screen8lim1fib11fib22fib_nextThewhileStatementChapter5RepetitionSchoolofInformationEngineering,NanchangHangkongUniversity23intfib1=1,fib2=1,fib_next=0;printf(%d,fib1);while(

1 / 97
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功