chapter 6: the repetition structure programming with microsoft visual basic 2005, third edition
TRANSCRIPT
2Programming with Microsoft Visual Basic 2005, Third Edition
The Repetition Structure (Looping)Lesson A Objectives
• Code the repetition structure using the For...Next and Do...Loop statements
• Include the repetition structure in pseudocode
• Include the repetition structure in a flowchart
• Initialize and update counters and accumulators
3Programming with Microsoft Visual Basic 2005, Third Edition
Previewing the Completed Application
• Go to Run command on Windows Start menu
• Browse to the VB2005\Chap06 folder
• Open the Shoppers.exe file
• The Shoppers Haven user interface appears
4Programming with Microsoft Visual Basic 2005, Third Edition
Previewing the Completed Application (continued)
Figure 6-1: Interface showing the discount and discounted price amounts
5Programming with Microsoft Visual Basic 2005, Third Edition
The Repetition Structure
• Repetition structure (loop)
– Repeatedly processes instructions until condition met
• Example: calculate net pay for each employee
• Pretest loop
– Condition evaluated prior to instruction processing
• Posttest loop
– Condition evaluated after instruction processing
6Programming with Microsoft Visual Basic 2005, Third Edition
The For…Next Statement
• For…Next statement
– Processes instructions a specific number of times
– Condition tested before processing (pretest loop)
– Also called a counter controlled loop
• Syntax and examples to follow
7Programming with Microsoft Visual Basic 2005, Third Edition
The For…Next Statement (continued)
Figure 6-2: Syntax and examples of the For...Next statement (continued) Page 485
8Programming with Microsoft Visual Basic 2005, Third Edition
The For…Next Statement (continued)
Figure 6-2: Syntax and examples of the For...Next statement
9Programming with Microsoft Visual Basic 2005, Third Edition
The For…Next Statement (continued)
• Syntactic Elements of For…Next statement
– Begins with the For clause, ends with Next clause
– counter: numeric variable tracking iterations
– startvalue and endvalue provide looping range
– stepvalue increments or decrements counter
– statements: processed in the body of the loop
• Hexagon: flowchart symbol representing For…Next
10Programming with Microsoft Visual Basic 2005, Third Edition
The For…Next Statement (continued)
Figure 6-5: Page 488, Pseudocode for the first example shown in Figure 6-2
11Programming with Microsoft Visual Basic 2005, Third Edition
The For…Next
Statement (continued)
Figure 6-6: Page 489: Flowchart for the first example shown in Figure 6-2
12Programming with Microsoft Visual Basic 2005, Third Edition
The Monthly Payment Calculator Application
• Task of xCalcButton’s Click event procedure– Calculate and display monthly car payments– Use term of five years and rates from 5 – 10%
• Basic structure of the For…Next statement– Use a procedure level variable, rate, as a counter– Set starting and ending values to 0.05 and 0.01– Set the step value to 0.1– Calculate monthly payment for current rate– Display interest rate and corresponding payment
13Programming with Microsoft Visual Basic 2005, Third Edition
The Monthly Payment Calculator Application (continued)
Figure 6-8: Page 491: Sample run of the application that contains the procedure
14Programming with Microsoft Visual Basic 2005, Third Edition
The Do…Loop Statement
• Do…Loop statement
– Codes both a pretest loop and a posttest loop
• Syntax and examples to follow
– Observe two variations of the syntax
– Variations correspond to pretest and posttest loop
15Programming with Microsoft Visual Basic 2005, Third Edition
The Do…Loop Statement (continued)
Figure 6-9: Page 493: Syntax and examples of the Do...Loop statement (continued)
16Programming with Microsoft Visual Basic 2005, Third Edition
The Do…Loop Statement (continued)
Figure 6-9: Syntax and examples of the Do...Loop statement
17Programming with Microsoft Visual Basic 2005, Third Edition
The Do…Loop Statement (continued)
• Syntactic elements for the Do…Loop statement– Begins with Do clause, ends with the Loop clause– Enter instructions to repeat between both clauses– Use either While or Until keyword before condition – Condition must evaluate to Boolean True or False
• Location of {While|Until} condition by syntax version– Pretest loop: appears in the Do clause– Posttest loop: appears in the Loop clause
• Diamond: represents loop condition in a flowchart
18Programming with Microsoft Visual Basic 2005, Third Edition
The Do…Loop Statement (continued)
Figure 6-13: Page 495: Pseudocode and flowchart for the posttest loop example shown in Figure 6-9 (continued)
19Programming with Microsoft Visual Basic 2005, Third Edition
The Do…Loop Statement (continued)
Figure 6-13: Pseudocode and flowchart for the posttest loop example shown in Figure 6-9
20Programming with Microsoft Visual Basic 2005, Third Edition
Using Counters and Accumulators
• Used to calculate subtotals, totals, and averages
• Counter: numeric variable used for counting
• Accumulator: variable used to tally various amounts
• Initialize: set initial value of counter or accumulator
• Updating (incrementing or decrementing)
– Changing value stored in counter or accumulator
– Update statement is used within a repetition structure
21Programming with Microsoft Visual Basic 2005, Third Edition
The Sales Express Application
• Objective: display average sales of company
• Structure of xCalcButton Click event procedure
– Use a pretest loop to retrieve each sales amount
– Update accumulator within loop to tally gross sales
– Update counter within loop to keep track of entries
– Exit loop after data entry has been completed
– If counter is > 0, calculate average sales amount
– Display the average sales amount
22Programming with Microsoft Visual Basic 2005, Third Edition
The Sales Express Application (continued)
Figure 6-15: Page 500: Pseudocode for the xCalcButton’s Click event procedure
23Programming with Microsoft Visual Basic 2005, Third Edition
Summary – Lesson A
• Repetition structure (loop): repeats a set of instructions until some condition is met
• Use a For...Next statement to code pretest loops
• Use a Do...Loop statement to code pretest and posttest loops
• Counters and accumulators must be initialized
• Counters and accumulators are updated in a loop
24Programming with Microsoft Visual Basic 2005, Third Edition
Nested Repetition StructuresLesson B Objectives
• Nest repetition structures
25Programming with Microsoft Visual Basic 2005, Third Edition
Nesting Repetition Structures
• Nested repetition structure
– Inner loop placed entirely within outer loop
– Placement of inner loop is known as nesting
• Clocks use nested loops to keep track of the time
• Analogizing minute and second hands to loops
– Outer loop corresponds to the minute hands
– Inner loop corresponds to the second hand
26Programming with Microsoft Visual Basic 2005, Third Edition
Nesting Repetition Structures (continued)
Figure 6-22: Nested loops used by a clock
27Programming with Microsoft Visual Basic 2005, Third Edition
Monthly Payment Calculator Application—
Nested For...Next Statements
• Objective: calculate and display car payments
• Use nested loops in xCalcButton’s Click event
• Role of the outer For…Next statement
– Control interest rates ranging from 5 - 10%
– Increment rates at each iteration by 1%
• Role of the inner For…Next statement
– Controls terms from 3 - 5 years
28Programming with Microsoft Visual Basic 2005, Third Edition
Monthly Payment Calculator Application—
Nested For...Next Statements (continued)
Figure 6-24: Monthly payments shown in the interface
29Programming with Microsoft Visual Basic 2005, Third Edition
Summary – Lesson B
• To nest a repetition structure, place the entire inner loop within the outer loop
30Programming with Microsoft Visual Basic 2005, Third Edition
Coding the Shoppers Haven Application
Lesson C Objectives
• Include a list box in an interface
• Select a list box item from code
• Determine the selected item in a list box
31Programming with Microsoft Visual Basic 2005, Third Edition
Shoppers Haven
• Objective: allow entry of price and discount rate
• Application requirements
– Discount rate range: 10% through 30%
– Discount rate should be incremented by 5%
– Calculate discount amount and discounted price
– Display discount amount and discounted price
32Programming with Microsoft Visual Basic 2005, Third Edition
Shoppers Haven (continued)
Figure 6-25: TOE chart for the Shoppers Haven application
33Programming with Microsoft Visual Basic 2005, Third Edition
Shoppers Haven (continued)
Figure 6-26: Partially completed user interface for the Shoppers Haven application
34Programming with Microsoft Visual Basic 2005, Third Edition
Including a List Box in an Interface
• List box– Displays a list of choices– User can select 0 or more items
• SelectionMode property– Controls number of choices that can be selected– Values: None, One, MultiSimple, or MultiExtended
• ListBox tool: used to add a list box to an interface
• List box can be made any size you want
35Programming with Microsoft Visual Basic 2005, Third Edition
Adding Items to a List Box
• Collection: group of objects treated as one unit
• Items collection– Refers to group of items in a list box– An index identifies each item in a collection
• Items collection’s Add method:– Specifies items you want displayed in a list box– Implemented in form’s Load event procedure
• Sorted property of a list box – Displays list box items in dictionary order when true
36Programming with Microsoft Visual Basic 2005, Third Edition
Adding Items to a List Box (continued)
Figure 6-28: Syntax and examples of the Add method
37Programming with Microsoft Visual Basic 2005, Third Edition
Adding Items to a List Box (continued)
Figure 6-29: Items added to the list boxes
38Programming with Microsoft Visual Basic 2005, Third Edition
The SelectedItem And SelectedIndex Properties
• SelectedItem property– Contains value of item selected in a list box– Value when no item is selected is the empty string
• SelectedIndex property – Contains index of item selected in a list box– Value when no item is selected is the number -1
• Default list box item– Appears when the application is first loaded– Can be chosen with SelectedItem and SelectedIndex
39Programming with Microsoft Visual Basic 2005, Third Edition
The SelectedItem And SelectedIndex Properties (continued)
Figure 6-34: Completed MainForm’s Load event procedure
40Programming with Microsoft Visual Basic 2005, Third Edition
The SelectedItem And SelectedIndex Properties (continued)
Figure 6-35: First item selected in the xRateListBox
41Programming with Microsoft Visual Basic 2005, Third Edition
Coding the Text Box’s TextChanged Event Procedure
• Control’s TextChanged event
– Occurs when contents of Text property change
• Additional requirement– Clear controls when TextChanged event occurs in
xOrigPriceTextBox
42Programming with Microsoft Visual Basic 2005, Third Edition
Coding the Text Box’s TextChanged Event Procedure (continued)
Figure 6-36: Completed ClearLabels procedure
43Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xCalcButton’s Click Event Procedure
• xCalcButton’s Click event procedure
– Last procedure to code in application
44Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xCalcButton’s Click Event Procedure (continued)
Figure 6-37: Pseudocode for the xCalcButton’s Click event procedure
45Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xCalcButton’s Click Event Procedure (continued)
Figure 6-39: Discount and discounted price amounts shown in the interface
46Programming with Microsoft Visual Basic 2005, Third Edition
Summary – Lesson C
• A list box displays a list of items
• Use list box’s SelectionMode property to set number of items to select in a list box
• List box items can be sorted in dictionary order
• Use Item collection’s Add method to specify items that will display in the list box
• Text box’s TextChanged event occurs when a change is made to the control’s contents