C語言基礎(chǔ)教程英文版ch05學(xué)習(xí)課程_第1頁
C語言基礎(chǔ)教程英文版ch05學(xué)習(xí)課程_第2頁
C語言基礎(chǔ)教程英文版ch05學(xué)習(xí)課程_第3頁
C語言基礎(chǔ)教程英文版ch05學(xué)習(xí)課程_第4頁
C語言基礎(chǔ)教程英文版ch05學(xué)習(xí)課程_第5頁
已閱讀5頁,還剩53頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、1Objectives Basic Loop Structures The while Statement Computing Sums and Averages Using a while Loop The for Statement第1頁/共58頁第一頁,編輯于星期五:十六點 四分。2Objectives (continued) Case Studies: Loop Programming Techniques Nested Loops The do-while Statement Common Programming and Compiler Errors第2頁/共58頁第二頁,編輯于星

2、期五:十六點 四分。3Introduction A section of code that is repeated is called a loop, because after the last statement in the code is executed, the program branches, or loops, back to the first statement and starts another repetition through the code Each repetition is also called an iteration or pass throug

3、h the loop第3頁/共58頁第三頁,編輯于星期五:十六點 四分。4Basic Loop Structures Constructing a repeating section of code requires that four elements be present: Repetition statement while statement for statement do-while statement Condition A statement that initially sets the condition being tested A statement within th

4、e repeating section of code that alters the condition so that it eventually becomes false第4頁/共58頁第四頁,編輯于星期五:十六點 四分。5Pretest and Posttest Loops第5頁/共58頁第五頁,編輯于星期五:十六點 四分。6Pretest and Posttest Loops (continued)第6頁/共58頁第六頁,編輯于星期五:十六點 四分。7Counter-Controlled and Condition-Controlled Loops Counter-controll

5、ed loop: the condition is used to keep track of the number of repetitions Also known as a fixed-count loop Condition-controlled loop: the tested condition does not depend on a count being achieved, but rather on a specific value being encountered第7頁/共58頁第七頁,編輯于星期五:十六點 四分。8Basic Loop Structures第8頁/共5

6、8頁第八頁,編輯于星期五:十六點 四分。9The while Statement The general form of the while statement iswhile (expression) statement; The transfer of control back to the start of a while statement to reevaluate the expression is known as a program loop The following is a valid but infinite loop:while (count = 10) printf

7、(%d ,count);第9頁/共58頁第九頁,編輯于星期五:十六點 四分。10The while Statement (continued)第10頁/共58頁第十頁,編輯于星期五:十六點 四分。11The while Statement (continued)Output is:1 2 3 4 5 6 7 8 9 10第11頁/共58頁第十一頁,編輯于星期五:十六點 四分。12The while Statement (continued)Output is:10 9 8 7 6 5 4 3 2 1第12頁/共58頁第十二頁,編輯于星期五:十六點 四分。13The while Statemen

8、t (continued)Output is:NUMBER SQUARE CUBE- - - 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000第13頁/共58頁第十三頁,編輯于星期五:十六點 四分。14Condition-controlled loopOutput is:DEGREES DEGREESCELSIUS FAHRENHEIT- - 5 41.00 10 50.00 15 59.00 20 68.00 25 77.00 30 86.00 35 95.00 40 104

9、.00 45 113.00 50 122.00The while Statement (continued)第14頁/共58頁第十四頁,編輯于星期五:十六點 四分。15Computing Sums and Averages Using a while Loop第15頁/共58頁第十五頁,編輯于星期五:十六點 四分。16Computing Sums and Averages Using a while Loop (continued)第16頁/共58頁第十六頁,編輯于星期五:十六點 四分。17Computing Sums and Averages Using a while Loop (cont

10、inued)第17頁/共58頁第十七頁,編輯于星期五:十六點 四分。18Computing Sums and Averages Using a while Loop (continued)第18頁/共58頁第十八頁,編輯于星期五:十六點 四分。19Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct valueAccumulating statementComp

11、uting Sums and Averages Using a while Loop (continued)第19頁/共58頁第十九頁,編輯于星期五:十六點 四分。20Calculating an averageComputing Sums and Averages Using a while Loop (continued)第20頁/共58頁第二十頁,編輯于星期五:十六點 四分。21Sentinels A program, such as Program 5.7, can be made much more general by removing the restriction that e

12、xactly four numbers are to be entered The user enters a value for how many numbers will be averaged You can use a sentinel (a data value used to signal either the start or end of a data series) The sentinel values must be selected so as not to conflict with legitimate data values第21頁/共58頁第二十一頁,編輯于星期

13、五:十六點 四分。22Sentinels (continued)第22頁/共58頁第二十二頁,編輯于星期五:十六點 四分。23Sentinels (continued) One useful sentinel in C is the named constant EOF (End Of File) The actual value of EOF is compiler-dependent, but it is always assigned a code that is not used by any other character EOF is defined in stdio.h第23頁/

14、共58頁第二十三頁,編輯于星期五:十六點 四分。24Sentinels (continued)第24頁/共58頁第二十四頁,編輯于星期五:十六點 四分。25Sentinels (continued)第25頁/共58頁第二十五頁,編輯于星期五:十六點 四分。26The break and continue Statements A break forces an immediate exit from while, switch, for, and do-while statements onlywhile(count 76) printf(You lose!); break; /* break

15、 out of the loop */ else printf(Keep on truckin!);/* break jumps to here */第26頁/共58頁第二十六頁,編輯于星期五:十六點 四分。27The break and continue Statements (continued)The continue applies to loops only; when a continue statement is encountered in a loop, the next iteration of the loop begins immediatelywhile (count

16、 30) printf(Enter a grade: ); scanf(%f, &grade); if(grade 100) continue; total = total + grade; count = count + 1;第27頁/共58頁第二十七頁,編輯于星期五:十六點 四分。28The Null Statement A semicolon with nothing preceding it is also a valid statement, called the null statement; Use the null statement where a statement

17、 is syntactically required, but no action is needed Null statements typically are used either with while or for statements第28頁/共58頁第二十八頁,編輯于星期五:十六點 四分。29The for Statement The for statement combines all four elements required to easily produce a loop on the same linefor (initializing list; tested exp

18、ression; altering list) statement; This statement does not require that any of the items in parentheses be present or that they actually be used for initializing or altering the values in the expression statements However, the two semicolons must be present for ( ; count = 20;) is valid Omitting tes

19、ted expression results in infinite loop第29頁/共58頁第二十九頁,編輯于星期五:十六點 四分。30The for Statement (continued)第30頁/共58頁第三十頁,編輯于星期五:十六點 四分。31The for Statement (continued) Output is:2 4 6 8 10 12 14 16 18 20第31頁/共58頁第三十一頁,編輯于星期五:十六點 四分。32The for Statement (continued)第32頁/共58頁第三十二頁,編輯于星期五:十六點 四分。33The for Stateme

20、nt (continued)第33頁/共58頁第三十三頁,編輯于星期五:十六點 四分。34The for Statement (continued)Comma-separated list第34頁/共58頁第三十四頁,編輯于星期五:十六點 四分。35The for Statement (continued)(compare with Program 5.3)第35頁/共58頁第三十五頁,編輯于星期五:十六點 四分。36Computing Sums and Averages Using a for Loop第36頁/共58頁第三十六頁,編輯于星期五:十六點 四分。37Case Studies:

21、Loop Programming Techniques Technique 1: Selection within a loop Technique 2: Input data validation Technique 3: Interactive loop control Technique 4: Evaluating equations第37頁/共58頁第三十七頁,編輯于星期五:十六點 四分。38Technique 1: Selection within a Loop第38頁/共58頁第三十八頁,編輯于星期五:十六點 四分。39Technique 2: Input Data Validat

22、ion Same code used in lines 6-7!第39頁/共58頁第三十九頁,編輯于星期五:十六點 四分。40Technique 2: Input Data Validation (continued)第40頁/共58頁第四十頁,編輯于星期五:十六點 四分。41Technique 3: Interactive Loop Control第41頁/共58頁第四十一頁,編輯于星期五:十六點 四分。42Technique 4: Evaluating Equations第42頁/共58頁第四十二頁,編輯于星期五:十六點 四分。43Technique 4: Evaluating Equat

23、ions (continued)第43頁/共58頁第四十三頁,編輯于星期五:十六點 四分。44Nested Loops第44頁/共58頁第四十四頁,編輯于星期五:十六點 四分。45Nested Loops (continued) Sample run:i is now 1j = 1 j = 2 j = 3 j = 4i is now 2j = 1 j = 2 j = 3 j = 4i is now 3j = 1 j = 2 j = 3 j = 4i is now 4j = 1 j = 2 j = 3 j = 4i is now 5j = 1 j = 2 j = 3 j = 4第45頁/共58頁

24、第四十五頁,編輯于星期五:十六點 四分。46Nested Loops (continued)第46頁/共58頁第四十六頁,編輯于星期五:十六點 四分。47Nested Loops (continued)第47頁/共58頁第四十七頁,編輯于星期五:十六點 四分。48The do-while Statement第48頁/共58頁第四十八頁,編輯于星期五:十六點 四分。49The do-while Statement (continued) The general form of the do statement isdo statement;while (expression); do-while

25、 is a posttest loop One type of application is ideally suited for a posttest loop: Input data validation application第49頁/共58頁第四十九頁,編輯于星期五:十六點 四分。50The do-while Statement (continued)do printf(nEnter an ID number: ); scanf(%f, &idNum); while (idNum 1999);第50頁/共58頁第五十頁,編輯于星期五:十六點 四分。51The do-while

26、Statement (continued)第51頁/共58頁第五十一頁,編輯于星期五:十六點 四分。52Common Programming Errors “Off by one” error, in which the loop executes either one too many or one too few times than intended Using the assignment operator, =, instead of the equality operator, =, in the tested expression As with the if statement

27、, repetition statements should not use the equality operator, =, when testing single-precision or double-precision operands第52頁/共58頁第五十二頁,編輯于星期五:十六點 四分。53Common Programming Errors (continued) Placing a semicolon at the end of the fors parentheses (creates a do-nothing loop) Using commas to separate the items in a for

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論