programming in ANSI C-Chapter 6 Decision Making and Looping_第1页
programming in ANSI C-Chapter 6 Decision Making and Looping_第2页
programming in ANSI C-Chapter 6 Decision Making and Looping_第3页
programming in ANSI C-Chapter 6 Decision Making and Looping_第4页
programming in ANSI C-Chapter 6 Decision Making and Looping_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

.,Chapter6DecisionMakingandLooping,PROGRAMMINGINANSIC,.,Question,Questions:Howdowecalculatethesumfrom1to5?Howdowecalculatethesumfrom1to100?,main()intsum=0;sum=sum+1;sum=sum+2;sum=sum+3;sum=sum+4;sum=sum+5;printf(sum=%d,sum);,main()intsum=0;sum=sum+1;sum=sum+2;sum=sum+3;sum=sum+100;printf(sum=%d,sum);,Letsfindoutthelawofsuchquestion:,Wheni=1,2,3,100do:sum=sum+i;,Loopconstruct!,.,Flowchart,Inalooping,asequenceofstatementsisrepetitivelyexecuteduntilthelooptestconditioncantbesatisfied.Aloopstatementconsistsof2segments:TheloopcontrolstatementTheloopbody,.,Chapter6,Inthischapter,wewilllearn:whilestatementdo.whilestatementforstatementAssistedcontrolstatements:breakandcontinueLoopingconsistsofifandgoto,.,whileStatement,Theformofwhilestatement:while(testcondition)loopbody,Thetestconditionisevaluatedfirst,andifitistrue,theloopbodyisexecuted.,.,whileStatement,Programtocalculatethesumfrom1to100.,main()inti=1,sum=0;while(i=100)sum=sum+i;i+;printf(%d,sum);,Iftheloopbodyconsistsofmorethanonestatement,weshouldusecompoundstatement.,.,whileStatement,Programtocalculatethesumfrom1to100.,main()inti=1,sum=0;while(i=100)sum=sum+i;i+;printf(%d,sum);,infiniteloop,Wecanusectrl+breaktostoptheexecutionofprogram.,main()inti=1,sum=0;while(i=100)sum=sum+i;i+;printf(%d,sum);,.,whileStatement2Segments,.,whileStatement2Segments,Theloopbodycanbeanytypestatement.Theloopbodyshouldbeasinglestatementoracompoundstatement.Theloopbodymustcontainsomestatementsmakingthelooptendtoend.Avoidinfiniteloop.,.,do.whileStatement,Theformofdo.whilestatement:doloopbodywhile(testcondition);,Theloopbodyisexecutedfirst,andthenthetestconditionisevaluated.,Indo.whilestatement,theloopbodyisalwaysexecutedatleastonce.,.,do.whileStatement,do.whilestatementcanchangetowhilestatement:,.,do.whileStatement,do.whilestatementcanchangetowhilestatement:,doloopbodywhile(testcondition);,loopbodywhile(testcondition)loopbody,.,do.whileStatement,Programtocalculatethesumfrom1to100.,main()inti=1,sum=0;dosum=sum+i;i+;while(i=100);printf(%d,sum);,Likewhilestatement,iftheloopbodyconsistsofmorethanonestatement,weshouldusecompoundstatement.,.,do.whileStatement2Segments,Theloopbodycanbeanytypestatement.Theloopbodyshouldbeasinglestatementoracompoundstatement.Theloopbodymustcontainsomestatementsmakeingthelooptendtoend.Avoidinfiniteloop.,Payattentiontothissemicolon!,.,whilescanf(%d,main()inti,sum=0;scanf(%d,15050,15050,1010,101101,whilethetestconditionisevaluatedfirstdo.whiletheloopbodyisevaluatedfirstwhilethetimesofexecutionofloopbody0dowhilethetimesofexecutionofloopbody1,.,forStatement,Theformofforstatement:for(exp1;exp2;exp3)loopbody,exp1,exp2andexp3areanytypeexpressions,andtheyallcanbeomitted.Buttheseparatorsemicoloncantbeomitted.for(;)isequivalenttowhile(1)infiniteloopforstatementcanbechangedtowhilestatement,.,forStatement,Theformofforstatement:for(exp1;exp2;exp3)loopbodyThegeneralapplicationformofforstatement:for(initialization;testcondition;increment)loopbodye.g.for(sum=0,i=1;i=100;i+)sum=sum+i;,isequivalentto:sum=0;i=1;while(i=100)sum=sum+i;i+;,.,forStatement,Theformofforstatement:for(exp1;exp2;exp3)loopbodyexp1andexp3canbecommaexpression.e.g.for(sum=0,i=1;i=100;sum+=i,i+);,isequivalentto:sum=0;for(i=1;i=100;i+)sum+=i;,.,forStatement,Programtocalculatethesumfrom1to100.,main()inti,sum;sum=0;for(i=1;i=100;i+)sum=sum+i;printf(%d,sum);,i=1;for(;i=100;i+)sum=sum+i;,i=1;for(;i100)break;sum=sum+(i+);,breakStatement,Programtocalculatethesumfrom1to100.,.,continueStatement,Form:continue;Function:Skipthefollowingstatementsinthisloop,causingthetestconditionofthenextlooptobejudged.,Thecontinuecanonlybeusedinloops.,.,continueStatement,continue;,.,continueStatement,Outputthenumbers,between100and200,whichcantbedividedexactlyby3.,main()intn;for(n=100;n=200;n+)if(n%3=0)continue;printf(%5d,n);,.,LoopsProgram1,Outputtheformer40numbersoftheFibonaccisequence.Fibonaccisequence:1,1,2,3,5,8,13,21,34,F1=1(n=1)F2=1(n=2)Fn=Fn-1+Fn-2(n3),f1f2?,.,LoopsProgram1,main()longf1=1,f2=1;inti;for(i=1;i=20;i+)printf(%12ld%12ld,f1,f2);if(i%2=0)printf(n);f1=f1+f2;f2=f2+f1;,i=1;while(i=20)printf(%12ld%12ld,f1,f2);if(i%2=0)printf(n);f1=f1+f2;f2=f2+f1;i+;,i=1;doprintf(%12ld%12ld,f1,f2);if(i%2=0)printf(n);f1=f1+f2;f2=f2+f1;i+;while(i=20);,.,LoopsProgram2,Judgewhetheranintegerisaprimenumberornot.Makembedividedbyallthenumbersbetween2andthesquarerootofm.Ifmcanbedividedexactlybyacertainnumberinthose,misnotaprimenumber,otherwiseitis.,.,LoopsProgram2,#includemain()intm,i,k;scanf(%d,Ifmcanbedividedexactlybythenumberbetween2andsqrt(m),misnotaprimenumber,sotheloopwillbeterminatedbybreakstatement,andatthattime,imustbelessorequaltok.Otherwisemisaprimenumber,andafterthelastloop,iisequaltok+1.,.,LoopsProgram2,#includemain()intm,i,k;scanf(%d,doif(m%i=0)break;i+;while(i=k);,for(i=2;i=k;i+)if(m%i=0)printf(%disnotaprimenumber,m);break;elseif(i=k)printf(%disaprimenumber,m);,Cantjudge1,2,3!,.,LoopsProgram3,Outputalltheprimesbetween100and200.,#includemain()intm,i,k,n=0;for(m=101;mk)printf(%5d,m);n=n+1;if(n%10=0)printf(n);,Trytousewhileanddowhilestatementtorewritethisprogram.,.,LoopsProgram4,Readinapositiveinteger,andoutputitinreversedorder.Forexample:readin12345,output54321.,#includemain()intn;printf(Inputapositiveinteger:);scanf(%d,/*numberisdecreasedby10times*/,.,LoopsProgram5,Outputthemultiplicationtable.,irepresentstheline:(1i9)jrepresentsthecolumn:(1ji),.,LoopsProgram5,main()inti,j;for(i=1;i10;i+)for(j=1;j=i;j+)printf(%d*%d=%dt,j,i,i*j);printf(n);,Trytouseothernestingofloopstorewritethisprogram.,.,LoopsProgram6,Readinintegern,andoutputnfactorial(n!).,main()intn;intm=1;printf(Pleaseinputanumber:);scanf(%d,Pleaseinputanumber:,5,printf(%d!=,n);,printf(%d,m);,0!=120,5!=120,Pleaseinputanumber:,8,8!=-25216,long,printf(%ld,m);,8!=40320,.,gotoStatement,Usedto:Jumpunconditionallyfromonepointtoanotherinaprogram.Generalforms:gotolabel;label:statement;orlabel:statement;gotolabel;,.,gotoStatement,Aboutlabel:Alabelmustbeavalididentifier,dontuseanintegerasalabel.Ala

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论