C++程序设计(6).ppt_第1页
C++程序设计(6).ppt_第2页
C++程序设计(6).ppt_第3页
C++程序设计(6).ppt_第4页
C++程序设计(6).ppt_第5页
已阅读5页,还剩78页未读 继续免费阅读

下载本文档

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

文档简介

11/12/2019,C+程序设计 教师:鲍钰,1,Chapter 6.,Looping,11/12/2019,C+程序设计 教师:鲍钰,2,A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly,What is a loop?,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,4,While Statement,SYNTAX while ( Expression ) . . / loop body . NOTE: Loop body can be a single statement, a null statement, or a block.,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,7,int count ; count = 4; / initialize loop variable while (count 0) / test expression cout count endl ; / repeated action count - ; / update loop variable cout “Done” endl ;,Count-controlled Loop,11/12/2019,C+程序设计 教师:鲍钰,8,Count-controlled Loop,int count ; count = 4; while (count 0) cout count endl ; count - ; cout “Done” endl ;,OUTPUT,11/12/2019,C+程序设计 教师:鲍钰,9,Count-controlled Loop,int count ; count = 4; while (count 0) cout count endl ; count - ; cout “Done” endl ;,OUTPUT,11/12/2019,C+程序设计 教师:鲍钰,10,Count-controlled Loop,int count ; count = 4; while (count 0) TRUE cout count endl ; count - ; cout “Done” endl ;,OUTPUT,11/12/2019,C+程序设计 教师:鲍钰,11,11/12/2019,C+程序设计 教师:鲍钰,12,11/12/2019,C+程序设计 教师:鲍钰,13,11/12/2019,C+程序设计 教师:鲍钰,14,11/12/2019,C+程序设计 教师:鲍钰,15,11/12/2019,C+程序设计 教师:鲍钰,16,11/12/2019,C+程序设计 教师:鲍钰,17,11/12/2019,C+程序设计 教师:鲍钰,18,11/12/2019,C+程序设计 教师:鲍钰,19,11/12/2019,C+程序设计 教师:鲍钰,20,11/12/2019,C+程序设计 教师:鲍钰,21,11/12/2019,C+程序设计 教师:鲍钰,22,11/12/2019,C+程序设计 教师:鲍钰,23,11/12/2019,C+程序设计 教师:鲍钰,24,myInfile contains 100 blood pressures Use a while loop to read the 100 blood pressures and find their total,Count-Controlled Loop Example,11/12/2019,C+程序设计 教师:鲍钰,25,ifstream myInfile ; int thisBP ; int total ; int count ; count = 0 ; / initialize while ( count thisBP ; total = total + thisBP ; count+ ; / update cout “The total = “ total endl ;,11/12/2019,C+程序设计 教师:鲍钰,26,Event-controlled Loops,Sentinel controlled keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop End-of-file controlled keep processing data as long as there is more data in the file Flag controlled keep processing data until the value of a flag changes in the loop body,11/12/2019,C+程序设计 教师:鲍钰,27,Examples of Kinds of Loops,Count controlled loop,Read exactly 100 blood pressures from a file.,End-of-file controlled loop,Read all the blood pressures from a file no matter how many are there.,11/12/2019,C+程序设计 教师:鲍钰,28,Examples of Kinds of Loops,Sentinel controlled loop,Read blood pressures until a special value (like -1) selected by you is read.,Flag controlled loop,Read blood pressures until a dangerously high BP (200 or more) is read.,11/12/2019,C+程序设计 教师:鲍钰,29,A Sentinel-controlled Loop,requires a “priming read” “priming read” means you read one set of data before the while,11/12/2019,C+程序设计 教师:鲍钰,30,Examples,cin month day ; / priming read while (!(month=2 ,11/12/2019,C+程序设计 教师:鲍钰,31,Examples,cin.get(inChar); while (inChar != n) cout inChar; cin.get(inChar); ,11/12/2019,C+程序设计 教师:鲍钰,32,/ Sentinel controlled loop total = 0; cout thisBP; while (thisBP != -1) / while not sentinel total = total + thisBP; cout thisBP; cout total;,11/12/2019,C+程序设计 教师:鲍钰,33,Examples,cindataValuesentinel; while ( sentinel = 1 ) / = cindataValuesentinel; / infinite loop,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,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;,11/12/2019,C+程序设计 教师:鲍钰,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 total;,11/12/2019,C+程序设计 教师:鲍钰,37,Flag-controlled Loops,you initialize a flag (to true or false) use meaningful name for the flag a condition in the loop body changes the value of the flag,11/12/2019,C+程序设计 教师:鲍钰,38,countGoodReadings = 0; isSafe = true; / initialize Boolean flag while (isSafe) cin thisBP; if ( thisBP = 200 ) isSafe = false; / change flag value else countGoodReadings+; cout countGoodReadings endl;,11/12/2019,C+程序设计 教师:鲍钰,39,Loops often used to,counting sum data values keep track of previous and current values,11/12/2019,C+程序设计 教师:鲍钰,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.,11/12/2019,C+程序设计 教师:鲍钰,41,Summing,int sum=0,count=1,number; while (countnumber; sum=sum+number; /summing count+; ,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,43,int count; char previous; char current; count = 0 ; inFile.get (previous); / priming reads inFile.get(current); while (inFile) if ( (current = =) / read another ,/counting != operator in a file,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,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.,11/12/2019,C+程序设计 教师:鲍钰,46,Another Example,int x=0,count=0; while (x100) int y=0; while (y10) count+; y+; x+; cout count endl; /count=?,11/12/2019,C+程序设计 教师:鲍钰,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 howMany Readings,11/12/2019,C+程序设计 教师:鲍钰,48,Read the data and display a chart,Patient ID BP Average,11/12/2019,C+程序设计 教师:鲍钰,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;,11/12/2019,C+程序设计 教师:鲍钰,50,myInfile.open(“A:BP.dat”); if (!myInfile ) / opening failed cout thisID howMany; / priming read,11/12/2019,C+程序设计 教师:鲍钰,51,while ( myInfile ) / last read successful patientCount+; cout thisBP; count +; totalForPatient = totalForPatient + thisBP; average = totalForPatient / float(howMany); cout thisID howMany; / another read ,11/12/2019,C+程序设计 教师:鲍钰,52,cout “There were “ patientCount “patients on file.” endl; cout “Program terminated.n”; return 0; ,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,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 ; ,11/12/2019,C+程序设计 教师:鲍钰,56,11/12/2019,C+程序设计 教师:鲍钰,57,Quick Check,p227-228,11/12/2019,C+程序设计 教师:鲍钰,58,Homework,p229-230 5,9,10,11/12/2019,C+程序设计 教师:鲍钰,59,PROGRAMMING,p232 8,10,12,11/12/2019,C+程序设计 教师:鲍钰,60,TRUE/FALSE,The logical order of statements in a program may be different from their physical order. A) True B) False,11/12/2019,C+程序设计 教师:鲍钰,61,TRUE/FALSE,TRUE,11/12/2019,C+程序设计 教师:鲍钰,62,TRUE/FALSE,The termination condition for the While loop while (loopCount 9. A) True B) False,11/12/2019,C+程序设计 教师:鲍钰,63,TRUE/FALSE,FALSE,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,65,TRUE/FALSE,FALSE,11/12/2019,C+程序设计 教师:鲍钰,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,11/12/2019,C+程序设计 教师:鲍钰,67,TRUE/FALSE,TRUE,11/12/2019,C+程序设计 教师:鲍钰,68,TRUE/FALSE,A program is said to be robust if it can recover from erroneous input and keep running. A) True B) False,11/12/2019,C+程序设计 教师:鲍钰,69,TRUE/FALSE,TRUE,11/12/2019,C+程序设计 教师:鲍钰,70,CHOICE,What is the termination condition for the following While loop? while (beta 0 A) beta 0 & beta = 0 & beta 10 D) beta = 10,11/12/2019,C+程序设计 教师:鲍钰,71,CHOICE,D,11/12/2019,C+程序设计 教师:鲍钰,72,CHOICE,Which type of loop would be most appropriate for solving the problem “Calculate the sum of all the odd integers in a data file of unknown length“? A) a count-controlled loop B) a flag-controlled loop C) a sentinel-controlled loop D) an EOF-controlled loop,11/12/2019,C+程序设计 教师:鲍钰,73,CHOICE,D,11/12/2019,C+程序设计 教师:鲍钰,74,CHOICE,Which type of loop would be most appropriate for solving the problem “Print every other input character until the character is encountered“? A) a count-controlled loop B) a flag-controlled loop C) a sentinel-controlled loop D) an EOF-controlled loop,11/12/2019,C+程序设计 教师:鲍钰,75,CHOICE,C,11/12/2019,C+程序设计 教师:鲍钰,76,CHOICE,Indicate where (if at all) the following loop needs a priming read. sum = 0; / Line 1 while (inFile) / Line 2 / Line 3 sum = sum + number; / Line 4 inFile number; / Line 5 / Line 6 A) between lines 1 and 2 B) between lines 2

温馨提示

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

评论

0/150

提交评论