已阅读5页,还剩37页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Chapter6DecisionMakingandLooping,PROGRAMMINGINANSIC,6/13/2020,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!,6/13/2020,Flowchart,Inalooping,asequenceofstatementsisrepetitivelyexecuteduntilthelooptestconditioncantbesatisfied.Aloopstatementconsistsof2segments:TheloopcontrolstatementTheloopbody,6/13/2020,Chapter6,Inthischapter,wewilllearn:whilestatementdo.whilestatementforstatementAssistedcontrolstatements:breakandcontinueLoopingconsistsofifandgoto,6/13/2020,whileStatement,Theformofwhilestatement:while(testcondition)loopbody,Thetestconditionisevaluatedfirst,andifitistrue,theloopbodyisexecuted.,6/13/2020,whileStatement,Programtocalculatethesumfrom1to100.,main()inti=1,sum=0;while(i=100)sum=sum+i;i+;printf(%d,sum);,Iftheloopbodyconsistsofmorethanonestatement,weshouldusecompoundstatement.,6/13/2020,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);,6/13/2020,whileStatement2Segments,6/13/2020,whileStatement2Segments,Theloopbodycanbeanytypestatement.Theloopbodyshouldbeasinglestatementoracompoundstatement.Theloopbodymustcontainsomestatementsmakingthelooptendtoend.Avoidinfiniteloop.,6/13/2020,do.whileStatement,Theformofdo.whilestatement:doloopbodywhile(testcondition);,Theloopbodyisexecutedfirst,andthenthetestconditionisevaluated.,Indo.whilestatement,theloopbodyisalwaysexecutedatleastonce.,6/13/2020,do.whileStatement,do.whilestatementcanchangetowhilestatement:,6/13/2020,do.whileStatement,do.whilestatementcanchangetowhilestatement:,doloopbodywhile(testcondition);,loopbodywhile(testcondition)loopbody,6/13/2020,do.whileStatement,Programtocalculatethesumfrom1to100.,main()inti=1,sum=0;dosum=sum+i;i+;while(i=100);printf(%d,sum);,Likewhilestatement,iftheloopbodyconsistsofmorethanonestatement,weshouldusecompoundstatement.,6/13/2020,do.whileStatement2Segments,Theloopbodycanbeanytypestatement.Theloopbodyshouldbeasinglestatementoracompoundstatement.Theloopbodymustcontainsomestatementsmakeingthelooptendtoend.Avoidinfiniteloop.,Payattentiontothissemicolon!,6/13/2020,whilescanf(%d,main()inti,sum=0;scanf(%d,15050,15050,1010,101101,whilethetestconditionisevaluatedfirstdo.whiletheloopbodyisevaluatedfirstwhilethetimesofexecutionofloopbody0dowhilethetimesofexecutionofloopbody1,6/13/2020,forStatement,Theformofforstatement:for(exp1;exp2;exp3)loopbody,exp1,exp2andexp3areanytypeexpressions,andtheyallcanbeomitted.Buttheseparatorsemicoloncantbeomitted.for(;)isequivalenttowhile(1)infiniteloopforstatementcanbechangedtowhilestatement,6/13/2020,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+;,6/13/2020,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;,6/13/2020,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.,6/13/2020,continueStatement,Form:continue;Function:Skipthefollowingstatementsinthisloop,causingthetestconditionofthenextlooptobejudged.,Thecontinuecanonlybeusedinloops.,6/13/2020,continueStatement,continue;,6/13/2020,continueStatement,Outputthenumbers,between100and200,whichcantbedividedexactlyby3.,main()intn;for(n=100;n=200;n+)if(n%3=0)continue;printf(%5d,n);,6/13/2020,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?,6/13/2020,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);,6/13/2020,LoopsProgram2,Judgewhetheranintegerisaprimenumberornot.Makembedividedbyallthenumbersbetween2andthesquarerootofm.Ifmcanbedividedexactlybyacertainnumberinthose,misnotaprimenumber,otherwiseitis.,6/13/2020,LoopsProgram2,#includemain()intm,i,k;scanf(%d,Ifmcanbedividedexactlybythenumberbetween2andsqrt(m),misnotaprimenumber,sotheloopwillbeterminatedbybreakstatement,andatthattime,imustbelessorequaltok.Otherwisemisaprimenumber,andafterthelastloop,iisequaltok+1.,6/13/2020,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!,6/13/2020,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.,6/13/2020,LoopsProgram4,Readinapositiveinteger,andoutputitinreversedorder.Forexample:readin12345,output54321.,#includemain()intn;printf(Inputapositiveinteger:);scanf(%d,/*numberisdecreasedby10times*/,6/13/2020,LoopsProgram5,Outputthemultiplicationtable.,irepresentstheline:(1i9)jrepresentsthecolumn:(1ji),6/13/2020,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.,6/13/2020,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,6/13/2020,gotoStatement,Usedto:Jumpunconditionallyfromonepointtoanotherinaprogram.Generalforms:gotolabel;label:statement;orlabel:statement;gotolabel;,6/13/2020,gotoStatement,Aboutlabel:Alabelmustbeavalididentifier,dontuseanintegerasalabel.Alabel
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 云南临沧市2026年招标采购从业人员专业技术能力考试(招标采购合同管理中级)测试题库及答案
- 大型网络监控施工方案
- 2026年天津市卫生健康委知情同意与医患沟通能力学习效果测试题(附答案)
- 2026年注册环保工程师考试(基础考试)全真模拟题库及答案(肇庆)
- 2026年度科技人才项目任务书
- 2026年环保工作总结及下一步工作安排
- 监控系统安装流程(视频监控安装教程)
- 2026年酒店安全管理员真题及参考答案
- 电力跨越施工专项方案
- 2026年大连市中山区(中小学、幼儿园)教师招聘笔试备考题库与答案详解
- 乡村振兴基础设施工程监理规划
- 沪教版小学六年级数学上册《方程与列方程》单元整体教案
- 2026年湖南汽车工程职业大学高职单招职业技能考试题库(夺冠系列)附答案详解
- 2026年新疆农牧业投资集团招聘面试题及答案
- 工程变更申请报告模板及填写规范
- 露天煤矿重大安全风险清单及管控措施
- 江苏省2025年普通高中学业水平合格性考试语文试卷 含答案
- 2026年高校教师资格证之高等教育学考试题库附参考答案【综合题】
- 胃肠解剖课件
- 【普通高中生物课程标准】日常修订版-(2017年版2025年修订)
- 空军招飞心理测试题及答案解析
评论
0/150
提交评论