C++程序设计PPT.ppt
《C++程序设计PPT.ppt》由会员分享,可在线阅读,更多相关《C++程序设计PPT.ppt(83页珍藏版)》请在咨信网上搜索。
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,3,*,3,1,Chapter 6.,Looping,3,2,A loop is a repetition control structure.,it causes a single statement or block to be executed repeatedly,What is a loop?,3,3,Two Types of Loops,count controlled loops,repeat a specified number of times,event-controlled loops,some condition within the loop body changes and this causes the repeating to stop,3,4,While Statement,SYNTAX,while (,Expression,),.,.,/loop body,.,NOTE:Loop body can be a single statement,a null statement,or a block.,3,5,When the expression is tested and found to be false,the loop is exited and control passes to the statement which follows the loop body,.,WHILE LOOP,FALSE,TRUE,body,statement,Expression,3,6,an initialization of the loop control variable,an expression to test for continuing the loop,an update of the loop control,variable to be executed with each iteration of the body,Count-controlled loop contains,3,7,int count;,count =4;,/initialize loop variable,while(count 0),/test expression,cout count endl;,/repeated action,count,-,;,/update loop variable,cout ,“,Done,”,0),cout count endl;,count,-,;,cout ,“,Done,”,0),cout count endl;,count,-,;,cout ,“,Done,”,0)TRUE,cout count endl;,count,-,;,cout ,“,Done,”,endl;,OUTPUT,count,4,3,11,3,12,3,13,3,14,3,15,3,16,3,17,3,18,3,19,3,20,3,21,3,22,3,23,3,24,myInfile contains 100 blood pressures,Use a while loop to read the 100 blood pressures and find their total,Count-Controlled Loop Example,3,25,ifstream myInfile;,int thisBP;,int total;,int count;,count=0;,/initialize,while (count thisBP;,total=total+thisBP;,count+;,/update,cout “The total=“total month day;,/priming read,while(!(month=2&day=3),/process date value,cinmonthday;,3,31,Examples,cin.get(inChar);,while(inChar!=n),cout inChar;,cin.get(inChar);,3,32,/Sentinel controlled loop,total=0;,cout thisBP;,while(thisBP!=-1),/while not sentinel,total=total+thisBP;,cout thisBP;,cout dataValuesentinel;,while(sentinel=1)/=,cindataValuesentinel;,/infinite loop,3,34,End-of-File Controlled Loop,depends on fact that a file goes into fail state when you try to read a data value beyond the end of the file,3,35,/End-of-file controlled loop,total=0;,myInfile thisBP;,/priming read,while(myInfile),/while last read successful,total=total+thisBP;,myInfile thisBP;,/read another,cout total;,3,36,/End-of-file at keyboard,total=0;,cout thisBP;,/priming read ,thisBP is int,while(cin),/while last read successful,total=total+thisBP;,cout thisBP;,/read another,cout thisBP;,if (thisBP=200),isSafe=false;,/change flag value,else,countGoodReadings+;,cout countGoodReadings endl;,3,39,Loops often used to,counting,sum data values,keep track of previous and current values,3,40,Counting,char inChar;int count=0;,cin.get(inChar);,while(inChar!=.),count+;/counting,cin.get(inChar);,/It counts the number of characters up to,but not including the period.,3,41,Summing,int sum=0,count=1,number;,while(countnumber;,sum=sum+number;/summing,count+;,3,42,Previous and Current Values,write a program that counts the number of!=operators in a program file,keep track of current and previous characters,3,43,int count;,char previous;,char current;,count=0;,inFile.get(previous);,/priming reads,inFile.get(current);,while(inFile),if(current,=,=,)&(previous,=,!,),count+;,previous=current;,/update,inFile.get(current);,/read another,/counting!=operator in a file,3,44,initialize outer loop,while (,outer loop condition,),.,initialize inner loop,while(,inner loop condition,),inner loop processing and update,.,Pattern of a Nested Loop,3,45,An Example,inFile.get(inChar);,while(inFile)/outer loop,int commaCount=0;,while(inChar!=n)/inner loop,if(inChar=,),commaCount+;,inFile.get(inChar);,cout commaCount endl;,inFile.get(inChar);,counting commas on a line of a file with many lines.,3,46,Another Example,int x=0,count=0;,while(x100),int y=0;,while(y10),count+;,y+;,x+;,cout count endl;,/count=?,3,47,Patient Data,A file contains blood pressure data for different people.Each line has a patient ID,the number of readings for that patient,followed by the actual readings.,ID howManyReadings,4567 5180 140 150 170 120,23182170 210,52323150 151 151,3,48,4567152,2318190,5232151,.,.,.,There were 432 patients in file.,Read the data and display a chart,Patient ID BP Average,3,49,#include#include,using namespace std;,int main()int patientCount;,/declarations,int thisID;int howMany;int thisBP;int totalForPatient;int count;float average;ifstream myInfile;,3,50,myInfile.open(,“,A:BP.dat,”,);if (!myInfile),/opening failed,cout,“,File opening error.Program terminated.,”,;return 1;cout thisID howMany;,/priming read,3,51,while(myInfile),/last read successful,patientCount+;cout thisID;,totalForPatient=0;,/initialize inner loop,count=0;,while(count thisBP;,count+;,totalForPatient=totalForPatient +thisBP;,average=totalForPatient/float(howMany);,cout int(average+.5)thisID howMany;,/another read,3,52,cout ,“,There were,“,patientCount,“,patients on file.,”,endl;,cout ,“,Program terminated.n,”,;,return 0;,3,53,Information About 20 Books in Diskfile,“A:myIn.dat”,3.98 P,7.41 H,8.79 P,.,.,.,Price of book,Hardback or,Paperback?,WRITE A PROGRAM TO FIND TOTAL VALUE OF ALL BOOKS,3,54,#include,/for cout,#include,/for file I/O,using namespace std;,int main(void),float price;,/declarations,char kind;,ifstream myInfile;,float total =0.0;,int count =1;,Program to Read Info about 20 Books From a Disk File,3,55,Rest of Program,myInfile.open(“A:myIn.dat”);,/count-controlled processing loop,while(count price kind;,total=total+price;,count+;,cout “Total is:“total endl;,myInfile.close();,return 0;,3,56,3,57,Quick Check,p227-228,3,58,Homework,p229-230,5,9,10,3,59,PROGRAMMING,p232,8,10,12,3,60,TRUE/FALSE,The logical order of statements in a program may be different from their physical order.,A)True,B)False,3,61,TRUE/FALSE,TRUE,3,62,TRUE/FALSE,The termination condition for the While loop,while(loopCount 9),cout loopCount 9.,A)True,B)False,3,63,TRUE/FALSE,FALSE,3,64,TRUE/FALSE,If a While loops termination condition becomes true in the middle of the loop body,the loop is exited immediately.,A)True,B)False,3,65,TRUE/FALSE,FALSE,3,66,TRUE/FALSE,In C+,an infinite loop results from using the assignment operator in the following way:,while(gamma=2),.,A)True,B)False,3,67,TRUE/FALSE,TRUE,3,68,TRUE/FALSE,A program is said to be robust if it can recover from erroneous input and keep running.,A)True,B)False,3,69,TRUE/FALSE,TRUE,3,70,CHOICE,What is the termination condition for the following While loop?,while(beta 0&beta 10),cout beta beta;,A)beta 0&beta=0&beta number;/Line 5,/Line 6,A)between lines 1 and 2,B)between lines 2 and 3,C)between lines 3 and 4,D)between lines 4 and 5,E)No priming read is necessary.,3,77,CHOICE,A,3,78,CHOICE,Given the input data,25 10 6-1,what is the output of the following code fragment?(All variables are of type int.),sum=0;,cin number;,while(number!=-1),cin number;,sum=sum+number;,cout sum endl;,A)15,B)41,C)40,D)16,E)no output-this is an infinite loop,3,79,CHOICE,A,3,80,CHOICE,After execution of the following code,what is the value of length?(count and length are of type int.),length=5;,count=4;,while(count=100),length=length-2;,else,length=count*length;,count+;,A)600,B)100,C)98,D)20,3,81,CHOICE,C,3,82,CHOICE,In the following code fragment,a semicolon appears at the end of the line containing the While condition.,cout A;,loopCount=1;,while(loopCount=3);,cout B;,loopCount+;,cout C;,The result will be:,A)the output AC,B)the output ABC,C)the output ABBBC,D)a compile-time error,E)an infinite loop,3,83,CHOICE,E,- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 程序设计 PPT
咨信网温馨提示:
1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前自行私信或留言给上传者【丰****】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时私信或留言给本站上传会员【丰****】,需本站解决可联系【 微信客服】、【 QQ客服】,若有其他问题请点击或扫码反馈【 服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【 版权申诉】”(推荐),意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:4008-655-100;投诉/维权电话:4009-655-100。
1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前自行私信或留言给上传者【丰****】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时私信或留言给本站上传会员【丰****】,需本站解决可联系【 微信客服】、【 QQ客服】,若有其他问题请点击或扫码反馈【 服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【 版权申诉】”(推荐),意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:4008-655-100;投诉/维权电话:4009-655-100。
关于本文