




已阅读5页,还剩53页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
A First Book of ANSI C Fourth Edition Chapter 5 Repetition A First Book of ANSI C, Fourth Edition2 Objectives Basic Loop Structures The while Statement Computing Sums and Averages Using a while Loop The for Statement A First Book of ANSI C, Fourth Edition3 Objectives (continued) Case Studies: Loop Programming Techniques Nested Loops The do-while Statement Common Programming and Compiler Errors A First Book of ANSI C, Fourth Edition4 Introduction 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 through the loop A First Book of ANSI C, Fourth Edition5 Basic 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 the repeating section of code that alters the condition so that it eventually becomes false A First Book of ANSI C, Fourth Edition6 Pretest and Posttest Loops A First Book of ANSI C, Fourth Edition7 Pretest and Posttest Loops (continued) A First Book of ANSI C, Fourth Edition8 Counter-Controlled and Condition -Controlled Loops Counter-controlled 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 A First Book of ANSI C, Fourth Edition9 Basic Loop Structures A First Book of ANSI C, Fourth Edition10 The while Statement The general form of the while statement is while (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 76) printf(“You lose!“); break; /* break out of the loop */ else printf(“Keep on truckin!“); /* break jumps to here */ A First Book of ANSI C, Fourth Edition28 The 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 immediately while (count 100) continue; total = total + grade; count = count + 1; A First Book of ANSI C, Fourth Edition29 The Null Statement A semicolon with nothing preceding it is also a valid statement, called the null statement ; Use the null statement where a statement is syntactically required, but no action is needed Null statements typically are used either with while or for statements A First Book of ANSI C, Fourth Edition30 The for Statement The for statement combines all four elements required to easily produce a loop on the same line for (initializing list; tested expression; 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 1999); A First Book of ANSI C, Fourth Edition52 The do-while Statement (continued) A First Book of ANSI C, Fourth Edition53 Common 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, repetition statements should not use the equality operator, =, when testing single-precision or double-precision operands A First Book of ANSI C, Fourth Edition54 Common 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 statement instead of the required semicolons Omitting the final semicolon from the do statement A First Book of ANSI C, Fourth Edition55 Common Compiler Errors A First Book of ANSI C, Fourth Edition56 Summary A section of repeating code is called a loop The three C repetition statements are while, for and do-while Loops are also classified as to the type of tested condition The most commonly used syntax for a while loop is while (expression) statements; A First Book of ANSI C, Fourth Edition57 Summary (continued) A for statement performs the same functions as the while statement, but uses a differ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版汽车吊车租赁及现场设备维护保养协议
- 二零二五年度模具制造企业委托加工合同样本
- 二零二五年度高铁站临时设施施工劳务合同
- 2025版公士绿化管理所生态修复与绿化一体化合同
- 二零二五年度家具企业厂长任期绩效考核合同
- 二零二五场地承包经营合同集锦:电竞产业园区版
- 二零二五版个人住房出租合同解除条件协议
- 2025版离婚协议申请范文:家庭财产分割参考模板
- 二零二五年度教育信息化产品供应合同
- 二零二五版全球货运信息化项目六租船合同
- 项目管理水电站项目基本情况
- 离职证明模板电子版
- GB/T 6980-1995钙塑瓦楞箱
- GB/T 26520-2011工业氯化钙
- GB/T 14691-1993技术制图字体
- 食材配送服务及应急保障方案
- 常见婚姻家庭纠纷及调解技巧课件
- 肠道微生物菌群与消化道肿瘤关系课件
- 2023年8月17日云南省临沧市遴选公务员笔试真题及解析
- 飞机火灾教案课件
- ISO37000-2021组织治理-指南(雷泽佳译2022)
评论
0/150
提交评论