- 1、本文档共34页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
ControlStructures-Repetition
Control Structures - Repetition Chapter 5 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled Loops Flag Controlled Loops EOF Controlled Loops for Loops The do … while Looping Structure Why Is Repetition Needed? Consider a task that the computer might automate Calculate and print paychecks Read invoices from a file and print monthly statements Read inputs from a sensor and adjust the air-fuel mixture of an engine In general The Input – process – output cycle Why Is Repetition Needed? The algorithm requires a set of actions to be done over and over again Same set of actions Number of times to be executed is unknown at design time We need a structure which allows Statement(s) to be repeated A method of testing to see whether the statements should be repeated The Repetition Structure The while loopprovides therepetition structure Statement(s) tobe repeated A means of checking whether the statement(s) will be repeated Syntax: while ( logicalExpression ) statement; Phases of Loop Execution Loop entry = flow of control reaches first statement inside loop Iteration = each pass thru the loop Loop test = condition tested before each iteration Loop exit = when termination condition occurs in while statement, loop is NOT executed another time While Loop Illustration Counter Controlled Loops For when you know exactly how many times the loop should run There are 10 employees for which to print checks There are 60 sensor inputs per second The program needs a Loop Control Variable Count Controlled Loop Uses a Loop Control Variable (LCV) Count Controlled Loops I nitialized I ncremented I nspected Count Controlled Loops Sentinel Controlled Loops Suppose you want to read some positive integers and average them You do not have a preset number of data items in mind. Suppose the number –999 marks the end of data. Then the -999 is your sentinel Flag Controlled Loops A flag controlled loop uses a Boo
文档评论(0)