Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: The Repetition Structure

Similar presentations


Presentation on theme: "Chapter 6: The Repetition Structure"— Presentation transcript:

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 Programming with Microsoft Visual Basic 2005, Third Edition

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 Programming with Microsoft Visual Basic 2005, Third Edition

4 Previewing the Completed Application (continued)
Figure 6-1: Interface showing the discount and discounted price amounts Programming with Microsoft Visual Basic 2005, Third Edition

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 Programming with Microsoft Visual Basic 2005, Third Edition

6 The 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 Programming with Microsoft Visual Basic 2005, Third Edition

7 The For…Next Statement (continued)
Figure 6-2: Syntax and examples of the For...Next statement (continued) Page 485 Programming with Microsoft Visual Basic 2005, Third Edition

8 The For…Next Statement (continued)
Figure 6-2: Syntax and examples of the For...Next statement Programming with Microsoft Visual Basic 2005, Third Edition

9 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 Programming with Microsoft Visual Basic 2005, Third Edition

10 The For…Next Statement (continued)
Figure 6-5: Page 488, Pseudocode for the first example shown in Figure 6-2 Programming with Microsoft Visual Basic 2005, Third Edition

11 The For…Next Statement (continued)
Figure 6-6: Page 489: Flowchart for the first example shown in Figure 6-2 Programming with Microsoft Visual Basic 2005, Third Edition

12 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 Programming with Microsoft Visual Basic 2005, Third Edition

13 The Monthly Payment Calculator Application (continued)
Figure 6-8: Page 491: Sample run of the application that contains the procedure Programming with Microsoft Visual Basic 2005, Third Edition

14 The Do…Loop Statement Do…Loop statement Syntax and examples to follow
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 Programming with Microsoft Visual Basic 2005, Third Edition

15 The Do…Loop Statement (continued)
Figure 6-9: Page 493: Syntax and examples of the Do...Loop statement (continued) Programming with Microsoft Visual Basic 2005, Third Edition

16 The Do…Loop Statement (continued)
Figure 6-9: Syntax and examples of the Do...Loop statement Programming with Microsoft Visual Basic 2005, Third Edition

17 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 Programming with Microsoft Visual Basic 2005, Third Edition

18 The Do…Loop Statement (continued)
Figure 6-13: Page 495: Pseudocode and flowchart for the posttest loop example shown in Figure 6-9 (continued) Programming with Microsoft Visual Basic 2005, Third Edition

19 The Do…Loop Statement (continued)
Figure 6-13: Pseudocode and flowchart for the posttest loop example shown in Figure 6-9 Programming with Microsoft Visual Basic 2005, Third Edition

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 Programming with Microsoft Visual Basic 2005, Third Edition

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 > 0, calculate average sales amount Display the average sales amount Programming with Microsoft Visual Basic 2005, Third Edition

22 The Sales Express Application (continued)
Figure 6-15: Page 500: Pseudocode for the xCalcButton’s Click event procedure Programming with Microsoft Visual Basic 2005, Third Edition

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 Programming with Microsoft Visual Basic 2005, Third Edition

24 Nested Repetition Structures Lesson B Objectives
Nest repetition structures Programming with Microsoft Visual Basic 2005, Third Edition

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 Programming with Microsoft Visual Basic 2005, Third Edition

26 Nesting Repetition Structures (continued)
Figure 6-22: Nested loops used by a clock Programming with Microsoft Visual Basic 2005, Third Edition

27 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 % Increment rates at each iteration by 1% Role of the inner For…Next statement Controls terms from years Programming with Microsoft Visual Basic 2005, Third Edition

28 Figure 6-24: Monthly payments shown in the interface
Monthly Payment Calculator Application— Nested For...Next Statements (continued) Figure 6-24: Monthly payments shown in the interface Programming with Microsoft Visual Basic 2005, Third Edition

29 Summary – Lesson B To nest a repetition structure, place the entire inner loop within the outer loop Programming with Microsoft Visual Basic 2005, Third Edition

30 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 Programming with Microsoft Visual Basic 2005, Third Edition

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 Programming with Microsoft Visual Basic 2005, Third Edition

32 Shoppers Haven (continued)
Figure 6-25: TOE chart for the Shoppers Haven application Programming with Microsoft Visual Basic 2005, Third Edition

33 Shoppers Haven (continued)
Figure 6-26: Partially completed user interface for the Shoppers Haven application Programming with Microsoft Visual Basic 2005, Third Edition

34 Including a List Box in an Interface
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 Programming with Microsoft Visual Basic 2005, Third Edition

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 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 Programming with Microsoft Visual Basic 2005, Third Edition

36 Adding Items to a List Box (continued)
Figure 6-28: Syntax and examples of the Add method Programming with Microsoft Visual Basic 2005, Third Edition

37 Adding Items to a List Box (continued)
Figure 6-29: Items added to the list boxes Programming with Microsoft Visual Basic 2005, Third Edition

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 Programming with Microsoft Visual Basic 2005, Third Edition

39 The SelectedItem And SelectedIndex Properties (continued)
Figure 6-34: Completed MainForm’s Load event procedure Programming with Microsoft Visual Basic 2005, Third Edition

40 The SelectedItem And SelectedIndex Properties (continued)
Figure 6-35: First item selected in the xRateListBox Programming with Microsoft Visual Basic 2005, Third Edition

41 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 Programming with Microsoft Visual Basic 2005, Third Edition

42 Coding the Text Box’s TextChanged Event Procedure (continued)
Figure 6-36: Completed ClearLabels procedure Programming with Microsoft Visual Basic 2005, Third Edition

43 Coding the xCalcButton’s Click Event Procedure
Last procedure to code in application Programming with Microsoft Visual Basic 2005, Third Edition

44 Coding the xCalcButton’s Click Event Procedure (continued)
Figure 6-37: Pseudocode for the xCalcButton’s Click event procedure Programming with Microsoft Visual Basic 2005, Third Edition

45 Coding the xCalcButton’s Click Event Procedure (continued)
Figure 6-39: Discount and discounted price amounts shown in the interface Programming with Microsoft Visual Basic 2005, Third Edition

46 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 Programming with Microsoft Visual Basic 2005, Third Edition


Download ppt "Chapter 6: The Repetition Structure"

Similar presentations


Ads by Google