欢迎来到天天文库
浏览记录
ID:58885918
大小:275.50 KB
页数:62页
时间:2020-09-30
《C语言程序设计现代方法课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter6Loops1Copyright©2008W.W.Norton&Company.Allrightsreserved.IterationStatementsC’siterationstatementsareusedtosetuploops.Aloopisastatementwhosejobistorepeatedlyexecutesomeotherstatement(theloopbody).InC,everyloophasacontrollingexpression.Eachtimetheloopbodyise
2、xecuted(aniterationoftheloop),thecontrollingexpressionisevaluated.Iftheexpressionistrue(hasavaluethat’snotzero)theloopcontinuestoexecute.Copyright©2008W.W.Norton&Company.Allrightsreserved.2IterationStatementsCprovidesthreeiterationstatements:Thewhilestatementisused
3、forloopswhosecontrollingexpressionistestedbeforetheloopbodyisexecuted.Thedostatementisusediftheexpressionistestedaftertheloopbodyisexecuted.Theforstatementisconvenientforloopsthatincrementordecrementacountingvariable.Copyright©2008W.W.Norton&Company.Allrightsreserv
4、ed.3ThewhileStatementUsingawhilestatementistheeasiestwaytosetupaloop.Thewhilestatementhastheformwhile(expression)statementexpressionisthecontrollingexpression;statementistheloopbody.Copyright©2008W.W.Norton&Company.Allrightsreserved.4ThewhileStatementExampleofawhil
5、estatement:while(i6、uallyhasthevaluezero.Copyright©2008W.W.Norton&Company.Allrightsreserved.5ThewhileStatementAwhilestatementthatcomputesthesmallestpowerof2thatisgreaterthanorequaltoanumbern:i=1;while(i7、now2.Isi8、fmultiplestatementsareneeded,usebracestocreateasinglecompoundstatement:while(i>0){printf("Tminus%dandcounting",i);i--;}Someprogrammersalwaysuse
6、uallyhasthevaluezero.Copyright©2008W.W.Norton&Company.Allrightsreserved.5ThewhileStatementAwhilestatementthatcomputesthesmallestpowerof2thatisgreaterthanorequaltoanumbern:i=1;while(i7、now2.Isi8、fmultiplestatementsareneeded,usebracestocreateasinglecompoundstatement:while(i>0){printf("Tminus%dandcounting",i);i--;}Someprogrammersalwaysuse
7、now2.Isi8、fmultiplestatementsareneeded,usebracestocreateasinglecompoundstatement:while(i>0){printf("Tminus%dandcounting",i);i--;}Someprogrammersalwaysuse
8、fmultiplestatementsareneeded,usebracestocreateasinglecompoundstatement:while(i>0){printf("Tminus%dandcounting",i);i--;}Someprogrammersalwaysuse
此文档下载收益归作者所有