Chapter 6: The Repetition Structure

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic 2008 Fourth Edition
Advertisements

Programming with Microsoft Visual Basic th Edition
1.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter Four The Selection Structure
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6: The Repetition Structure
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Computer Programming TCP1224 Chapter 8 More On Repetition Structure.
1.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 16 I’m on the Inside; You’re on the Outside.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 14 Do It, Then Ask Permission.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
Microsoft Visual Basic 2008: Reloaded Third Edition
REPETITION CONTROL STRUCTURE
Clearly Visual Basic: Programming with Visual Basic nd Edition
Repeating Program Instructions
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Chapter 8: More on the Repetition Structure
Presentation transcript:

Chapter 6: The Repetition Structure Programming with Microsoft Visual Basic .NET, Second Edition

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 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 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 Programming with Microsoft Visual Basic .NET, Second Edition

The For…Next Loop Use the For…Next statement to code a loop that repeats for a specific number of times Figure 6-2: Syntax and examples of the For...Next statement Programming with Microsoft Visual Basic .NET, Second Edition

The For…Next Loop (continued) Figure 6-2: Syntax and examples of the For...Next statement (continued) Programming with Microsoft Visual Basic .NET, Second Edition

The For…Next 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 Programming with Microsoft Visual Basic .NET, Second Edition

The For…Next Loop (continued) Figure 6-4: Pseudocode and flowchart for the first example shown in Figure 6-2 Programming with Microsoft Visual Basic .NET, Second Edition

The For…Next Loop (continued) For…Next loop examples: Dim count As Integer For count = 0 to 3 Step 1 Debug.WriteLine(count) Next count For count = 3 to 0 Step -1 For count = 0 to 10 Step 2 Dim loc As Single For loc = 0.5 To 15 Step 0.5 Debug.WriteLine(loc) Next loc Programming with Microsoft Visual Basic .NET, Second Edition

The Do…Loop Statement Unlike the For…Next statement, the Do…Loop statement can be used to code both a pretest loop and a posttest loop The Do…Loop statement begins with the Do clause and ends with the Loop clause Programming with Microsoft Visual Basic .NET, Second Edition

The Do…Loop Statement (continued) Figure 6-7: Syntax and examples of the Do...Loop statement Programming with Microsoft Visual Basic .NET, Second Edition

The Do…Loop Statement (continued) Figure 6-7: Syntax and examples of the Do...Loop statement (continued) Programming with Microsoft Visual Basic .NET, Second Edition

The Do…Loop Statement (continued) Figure 6-9: Flowcharts for the examples shown in Figure 6-7 Programming with Microsoft Visual Basic .NET, Second Edition

The Do…Loop Statement (continued) Figure 6-9: Flowcharts for the examples shown in Figure 6-7 (continued) 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 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 Programming with Microsoft Visual Basic .NET, Second Edition

Nested Repetition Structures Lesson B Objectives Nest repetition structures 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 Programming with Microsoft Visual Basic .NET, Second Edition

Nesting Repetition Structures (continued) Figure 6-16: Nested loops used by a clock 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 student’s 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 Programming with Microsoft Visual Basic .NET, Second Edition

The Grade Calculator Application (continued) Total points earned Grade 270–300 A 240–269 B 210–239 C 180–209 D below 180 F Programming with Microsoft Visual Basic .NET, Second Edition

The Grade Calculator Application (continued) uiAssignGradeButton’s Click event procedure Allows Professor Arkins to enter each student’s test scores, and then assign the appropriate grade Contains two loops, one nested within the other A For...Next statement controls the inner loop Programming with Microsoft Visual Basic .NET, Second Edition

The Grade Calculator Application (continued) uiAssignGradeButton’s 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 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 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 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 item’s original price The application should display the discount rates and the discounted prices in the interface Programming with Microsoft Visual Basic .NET, Second Edition

Shoppers Haven (continued) Figure 6-21: User interface for the Shoppers Haven application Programming with Microsoft Visual Basic .NET, Second Edition

Shoppers Haven (continued) Figure 6-22: TOE chart for the Shoppers Haven application Programming with Microsoft Visual Basic .NET, Second Edition

Shoppers Haven (continued) Figure 6-23: Pseudocode for the Calculate button’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition

Shoppers Haven (continued) Figure 6-27: Discounted prices shown in the Shoppers Haven application 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 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 control’s Enter event A text box control’s 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 control’s Enter event is responsible for highlighting the existing text in the control 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 Programming with Microsoft Visual Basic .NET, Second Edition

Coding the TextChanged Event Procedure A control’s TextChanged event occurs when the contents of a control’s Text property change Use the uiOriginalTextBox’s TextChanged event to clear the contents of the uiDiscPricesLabel when the user changes the original price Programming with Microsoft Visual Basic .NET, Second Edition

Coding the ShoppersForm’s Closing Event Procedure A form’s 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 user’s response Programming with Microsoft Visual Basic .NET, Second Edition

Coding the ShoppersForm’s Closing Event Procedure (continued) Figure 6-31: Pseudocode for the ShoppersForm’s Closing event procedure Programming with Microsoft Visual Basic .NET, Second Edition

Coding the ShoppersForm’s Closing Event Procedure (continued) Figure 6-33: Message box displayed by the form’s Closing event Programming with Microsoft Visual Basic .NET, Second Edition

Summary Repetition structure (loop): the computer repeats a set of instructions until some condition is met Code a repetition structure in Visual Basic .NET using one of the following statements: For...Next, Do...Loop, and For Each…Next The For...Next statement is pretest loops only The Do...Loop statement can code pretest and posttest loops Programming with Microsoft Visual Basic .NET, Second Edition

Summary (continued) To use a counter or accumulator: Initialize, if necessary Update using an assignment statement in a repetition structure To nest a repetition structure, place the entire inner loop within the outer loop Programming with Microsoft Visual Basic .NET, Second Edition

Summary (continued) To process code when the user tabs to a control, or when the Focus method is used in code to send the focus to the control, enter the code in the control’s Enter event procedure To process code when a form is about to be closed, enter the code in the form’s Closing event procedure Programming with Microsoft Visual Basic .NET, Second Edition