




已阅读5页,还剩35页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Chapter 6: The Repetition Structure Programming with Microsoft Visual Basic .NET, Second Edition 1 The Repetition Structure (Looping) Lesson A Objectives Code the repetition structure using the For.Next and Do.Loop statements Write pseudocode for the repetition structure Create a flowchart for the repetition structure Initialize and update counters and accumulators 2 Programming with Microsoft Visual Basic .NET, Second Edition The Repetition Structure Use the repetition structure to repeatedly process one or more program instructions until some condition is met, at which time the repetition ends The repetition structure is referred to as a loop 3 Programming with Microsoft Visual Basic .NET, Second Edition The Repetition Structure (continued) Pretest loop: evaluation occurs before the instructions within the loop are processed Posttest loop: evaluation occurs after the instructions within the loop are processed 4 Programming with Microsoft Visual Basic .NET, Second Edition The ForNext Loop Use the ForNext statement to code a loop that repeats for a specific number of times Figure 6-2: Syntax and examples of the For.Next statement 5 Programming with Microsoft Visual Basic .NET, Second Edition The ForNext Loop (continued) Figure 6-2: Syntax and examples of the For.Next statement (continued) 6 Programming with Microsoft Visual Basic .NET, Second Edition The ForNext Loop (continued) counter is a numeric variable that keeps track of how many times the loop instructions are repeated startvalue, endvalue, and stepvalue Must be numeric Can be positive or negative, integer or non-integer Default stepvalue is 1 7 Programming with Microsoft Visual Basic .NET, Second Edition The ForNext Loop (continued) Figure 6-4: Pseudocode and flowchart for the first example shown in Figure 6-2 8 Programming with Microsoft Visual Basic .NET, Second Edition The ForNext Loop (continued) Dim count As Integer For count = 0 to 3 Step 1 Debug.WriteLine(co unt) Next count Dim count As Integer For count = 3 to 0 Step - 1 Debug.WriteLine(coun t) Next count Dim count As Integer For count = 0 to 10 Step 2 Debug.WriteLine(co unt) Next count Dim loc As Single For loc = 0.5 To 15 Step 0.5 Debug.WriteLine(loc) Next loc ForNext loop examples: 9 Programming with Microsoft Visual Basic .NET, Second Edition The DoLoop Statement Unlike the ForNext statement, the DoLoop statement can be used to code both a pretest loop and a posttest loop The DoLoop statement begins with the Do clause and ends with the Loop clause 10 Programming with Microsoft Visual Basic .NET, Second Edition The DoLoop Statement (continued) Figure 6-7: Syntax and examples of the Do.Loop statement 11 Programming with Microsoft Visual Basic .NET, Second Edition The DoLoop Statement (continued) Figure 6-7: Syntax and examples of the Do.Loop statement (continued) 12 Programming with Microsoft Visual Basic .NET, Second Edition The DoLoop Statement (continued) Figure 6-9: Flowcharts for the examples shown in Figure 6-7 13 Programming with Microsoft Visual Basic .NET, Second Edition The DoLoop Statement (continued) Figure 6-9: Flowcharts for the examples shown in Figure 6-7 (continued) 14 Programming with Microsoft Visual Basic .NET, Second Edition Using Counters and Accumulators Counters and accumulators are used within a repetition structure to calculate subtotals, totals, and averages A counter is a numeric variable used for counting something and is typically updated by 1 An accumulator is a numeric variable used for accumulating and is updated by an amount that varies 15 Programming with Microsoft Visual Basic .NET, Second Edition Using Counters and Accumulators (continued) Initializing: assigning a beginning value to the counter or accumulator Updating (incrementing): adding a number to the value stored in the counter or accumulator 16 Programming with Microsoft Visual Basic .NET, Second Edition Nested Repetition Structures Lesson B Objectives Nest repetition structures 17 Programming with Microsoft Visual Basic .NET, Second Edition Nesting Repetition Structures In a nested repetition structure, one loop, referred to as the inner loop, is placed entirely within another loop, called the outer loop A clock uses nested loops to keep track of the time 18 Programming with Microsoft Visual Basic .NET, Second Edition Nesting Repetition Structures (continued) Figure 6-16: Nested loops used by a clock 19 Programming with Microsoft Visual Basic .NET, Second Edition The Grade Calculator Application Professor Arkins needs an application that allows him to assign a grade to any number of students Each students grade is based on three test scores, with each test worth 100 points The application should total the test scores and then assign the appropriate grade, using the table shown on the next slide 20 Programming with Microsoft Visual Basic .NET, Second Edition The Grade Calculator Application (continued) Total points earnedGrade 270300A 240269B 210239C 180209D below 180F 21 Programming with Microsoft Visual Basic .NET, Second Edition The Grade Calculator Application (continued) uiAssignGradeButtons Click event procedure Allows Professor Arkins to enter each students test scores, and then assign the appropriate grade Contains two loops, one nested within the other A For.Next statement controls the inner loop 22 Programming with Microsoft Visual Basic .NET, Second Edition The Grade Calculator Application (continued) uiAssignGradeButtons Click event procedure (continued) A Do.Loop statement controls the outer loop The inner loop is a pretest loop The outer loop is a posttest loop 23 Programming with Microsoft Visual Basic .NET, Second Edition The Grade Calculator Application (continued) Figure 6-20: Sample run of the application that contains the procedure 24 Programming with Microsoft Visual Basic .NET, Second Edition Coding the Shoppers Haven Application Lesson C Objectives Select the existing text in a text box Prevent a form from closing 25 Programming with Microsoft Visual Basic .NET, Second Edition Shoppers Haven The manager of Shoppers Haven wants an application that the store clerks can use to calculate the discounted price of an item, using discount rates from 10% through 30% in increments of 5% The clerks will enter the items original price The application should display the discount rates and the discounted prices in the interface 26 Programming with Microsoft Visual Basic .NET, Second Edition Shoppers Haven (continued) Figure 6-21: User interface for the Shoppers Haven application 27 Programming with Microsoft Visual Basic .NET, Second Edition Shoppers Haven (continued) Figure 6-22: TOE chart for the Shoppers Haven application 28 Programming with Microsoft Visual Basic .NET, Second Edition Shoppers Haven (continued) Figure 6-23: Pseudocode for the Calculate buttons Click event procedure 29 Programming with Microsoft Visual Basic .NET, Second Edition Shoppers Haven (continued) Figure 6-27: Discounted prices shown in the Shoppers Haven application 30 Programming with Microsoft Visual Basic .NET, Second Edition Selecting the Existing Text in a Text Box Use the SelectAll method to select all of the text contained in a text box Syntax: textbox.SelectAll() textbox is the name of the text box whose text you want to select 31 Programming with Microsoft Visual Basic .NET, Second Edition Selecting the Existing Text in a Text Box (continued) Enter the SelectAll method in a text box controls Enter event A text box controls Enter event occurs when the user tabs to the control, and when the Focus method is used in code to send the focus to the control The uiOriginalTextBox controls Enter event is responsible for highlighting the existing text in the control 32 Programming with Microsoft Visual Basic .NET, Second Edition Selecting the Existing Text in a Text Box (continued) Figure 6-29: Text selected in the Shoppers Haven application 33 Programming with Microsoft Visual Basic .NET, Second Edition Coding the TextChanged Event Procedure A controls TextChanged event occurs when the contents of a controls Text property change Use the uiOriginalTextBoxs TextChanged event to clear the contents of the uiDiscPricesLabel when the user changes the original price 34 Programming with Microsoft Visual Basic .NET, Second Edition Coding the ShoppersForms Closing Event Procedure A forms Closing event occurs when a form is about to be closed In the Shoppers Haven application, the Closing event procedure is responsible for: Verifying that the user wants to exit the application Taking an action based on the users response 35 Programming with Microsoft Visual Basic .NET, Second Edition Coding the ShoppersForms Closing Event Procedure (continued) Figure 6-31: Pseudocode for the ShoppersForms Closing event procedure 36 Programming with Microsoft Visual Basic .NET, Second Edition Coding the ShoppersForms Closing Event Procedure (continued) Figure 6-33: Message box displayed by the forms Closing event 37 Programming with Microsoft Visual Basic .NET, Second Edition Summary Repetition structure (loop): the co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 环保设备安装维护服务合同
- 校园餐管理绩效评估与持续改进机制
- 传统工程教育模式的局限性及挑战
- 健康保险行业理赔数据表
- 互联网食品销售平台合作合同
- 乡村人口健康需求与风险评估
- 2025年心理学实验设计与统计分析考试试题及答案
- 2025年人际关系与沟通能力测试试题及答案
- 2025年教师资格证考试试卷及答案
- 2025年健康心理学考研入学考试试卷及答案
- 2025届重庆市普通高中学业水平选择性考试预测历史试题(含答案)
- 人教版小学语文四年级下册作文范文2
- 大学语文试题及答案琴
- 实验题(7大类42题)原卷版-2025年中考化学二轮复习热点题型专项训练
- CJ/T 362-2011城镇污水处理厂污泥处置林地用泥质
- 红十字会资产管理制度
- 2025安全宣传咨询日活动知识手册
- DB31/T 1249-2020医疗废物卫生管理规范
- 四川省宜宾市翠屏区2025届数学七下期末综合测试试题含解析
- 乡镇合法性审查工作报告
- 2025年发展对象考试题题库及答案
评论
0/150
提交评论