




已阅读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-2030蓝牙车载耳机行业发展分析及投资价值研究咨询报告
- 2025-2030芝麻油行业市场深度调研及发展趋势与投资战略研究报告
- 黑骏马测试题及答案
- 药品不良反应的临床观察试题及答案
- 消除紧张情绪的护士资格证考试试题及答案
- 食管癌护理教学查房
- 重症脑卒中患者的监测及管理
- 西医临床考试试题自我评估试题及答案
- 2025年碱锰电池项目发展计划
- 知识整合税务师试题及答案
- 膜系设计结构及调试
- 35kv配电系统继电保护方案设计(共33页)
- 中国收藏家协会个人会员入会申请表
- 文件模板(平行文)
- 漱口水公司绩效计划(范文)
- Theme and Rheme 主位与述位(课堂PPT)
- 压力容器设计计算书
- 尿毒症脑病ppt课件
- 部编版四年级下册语文课件-第三单元-单元解读-共64张PPT)
- 医院处方笺模板
- 公司部门职能介绍
评论
0/150
提交评论