Chapter 6: The Repetition Structure

1 / 46
About This Presentation
Title:

Chapter 6: The Repetition Structure

Description:

Accumulator: variable used to tally various amounts ... Changing value stored in counter or accumulator ... Update accumulator within loop to tally gross sales ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0

less

Transcript and Presenter's Notes

Title: Chapter 6: The Repetition Structure


1
Chapter 6 The Repetition Structure
Programming with Microsoft Visual Basic 2005,
Third Edition
2
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

3
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

4
Previewing the Completed Application (continued)
Figure 6-1 Interface showing the discount and
discounted price amounts
5
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

6
The ForNext Statement
  • ForNext 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

7
The ForNext Statement (continued)
Figure 6-2 Syntax and examples of the For...Next
statement (continued)
8
The ForNext Statement (continued)
Figure 6-2 Syntax and examples of the For...Next
statement
9
The ForNext Statement (continued)
  • Syntactic Elements of ForNext 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 ForNext

10
The ForNext Statement (continued)
Figure 6-5 Pseudocode for the first example
shown in Figure 6-2
11
The ForNext Statement (continued)
Figure 6-6 Flowchart for the first example shown
in Figure 6-2
12
The Monthly Payment Calculator Application
  • Task of xCalcButtons Click event procedure
  • Calculate and display monthly car payments
  • Use term of five years and rates from 5 10
  • Basic structure of the ForNext 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

13
The Monthly Payment Calculator Application
(continued)
Figure 6-8 Sample run of the application that
contains the procedure
14
The DoLoop Statement
  • DoLoop 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

15
The DoLoop Statement (continued)
Figure 6-9 Syntax and examples of the Do...Loop
statement (continued)
16
The DoLoop Statement (continued)
Figure 6-9 Syntax and examples of the Do...Loop
statement
17
The DoLoop Statement (continued)
  • Syntactic elements for the DoLoop 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 WhileUntil 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

18
The DoLoop Statement (continued)
Figure 6-13 Pseudocode and flowchart for the
posttest loop example shown in Figure 6-9
(continued)
19
The DoLoop Statement (continued)
Figure 6-13 Pseudocode and flowchart for the
posttest loop example shown in Figure 6-9
20
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

21
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 gt 0, calculate average sales amount
  • Display the average sales amount

22
The Sales Express Application (continued)
Figure 6-15 Pseudocode for the xCalcButtons
Click event procedure
23
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

24
Nested Repetition StructuresLesson B Objectives
  • Nest repetition structures

25
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

26
Nesting Repetition Structures (continued)
Figure 6-22 Nested loops used by a clock
27
Monthly Payment Calculator ApplicationNested
For...Next Statements
  • Objective calculate and display car payments
  • Use nested loops in xCalcButtons Click event
  • Role of the outer ForNext statement
  • Control interest rates ranging from 5 - 10
  • Increment rates at each iteration by 1
  • Role of the inner ForNext statement
  • Controls terms from 3 - 5 years

28
Monthly Payment Calculator ApplicationNested
For...Next Statements (continued)
Figure 6-24 Monthly payments shown in the
interface
29
Summary Lesson B
  • To nest a repetition structure, place the entire
    inner loop within the outer loop

30
Coding the Shoppers Haven ApplicationLesson C
Objectives
  • Include a list box in an interface
  • Select a list box item from code
  • Determine the selected item in a list box

31
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

32
Shoppers Haven (continued)
Figure 6-25 TOE chart for the Shoppers Haven
application
33
Shoppers Haven (continued)
Figure 6-26 Partially completed user interface
for the Shoppers Haven application
34
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

35
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 collections Add method
  • Specifies items you want displayed in a list box
  • Implemented in forms Load event procedure
  • Sorted property of a list box
  • Displays list box items in dictionary order when
    true

36
Adding Items to a List Box (continued)
Figure 6-28 Syntax and examples of the Add method
37
Adding Items to a List Box (continued)
Figure 6-29 Items added to the list boxes
38
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

39
The SelectedItem And SelectedIndex Properties
(continued)
Figure 6-34 Completed MainForms Load event
procedure
40
The SelectedItem And SelectedIndex Properties
(continued)
Figure 6-35 First item selected in the
xRateListBox
41
Coding the Text Boxs TextChanged Event Procedure
  • Controls TextChanged event
  • Occurs when contents of Text property change
  • Additional requirement
  • Clear controls when TextChanged event occurs in
    xOrigPriceTextBox

42
Coding the Text Boxs TextChanged Event Procedure
(continued)
Figure 6-36 Completed ClearLabels procedure
43
Coding the xCalcButtons Click Event Procedure
  • xCalcButtons Click event procedure
  • Last procedure to code in application

44
Coding the xCalcButtons Click Event Procedure
(continued)
Figure 6-37 Pseudocode for the xCalcButtons
Click event procedure
45
Coding the xCalcButtons Click Event Procedure
(continued)
Figure 6-39 Discount and discounted price
amounts shown in the interface
46
Summary Lesson C
  • A list box displays a list of items
  • Use list boxs SelectionMode property to set
    number of items to select in a list box
  • List box items can be sorted in dictionary order
  • Use Item collections Add method to specify items
    that will display in the list box
  • Text boxs TextChanged event occurs when a change
    is made to the controls contents
Write a Comment
User Comments (0)