收藏 分销(赏)

C++程序设计PPT.ppt

上传人:丰**** 文档编号:7480471 上传时间:2025-01-06 格式:PPT 页数:83 大小:522.50KB
下载 相关 举报
C++程序设计PPT.ppt_第1页
第1页 / 共83页
C++程序设计PPT.ppt_第2页
第2页 / 共83页
C++程序设计PPT.ppt_第3页
第3页 / 共83页
C++程序设计PPT.ppt_第4页
第4页 / 共83页
C++程序设计PPT.ppt_第5页
第5页 / 共83页
点击查看更多>>
资源描述

1、,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,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,

2、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 cont

3、rol 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-contr

4、olled 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;,co

5、unt,-,;,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;,i

6、nt 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

7、,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 th

8、e 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 la

9、st 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;

10、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 cou

11、nts 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(cur

12、rent);,/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

13、;,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

14、,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.

15、,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 fa

16、iled,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=tota

17、lForPatient/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,Pa

18、perback?,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 Prog

19、ram,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 o

20、f 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 beco

21、mes 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

22、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 li

23、nes 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;,cou

24、t 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

25、,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,

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服