Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Six The Repetition Structure.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Six The Repetition Structure."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Six The Repetition Structure

2 Previewing the Shoppers Haven Application Open the Shoppers.exe file The Shoppers Haven application: –Allows user to enter an item’s original price and its discount rate –Calculates and displays amount of discount and discounted price 2Programming with Microsoft Visual Basic 2008, Fourth Edition

3 Previewing the Shoppers Haven Application (continued) Figure 6-1: Discount and discounted price shown in the interface 3Programming with Microsoft Visual Basic 2008, Fourth Edition

4 Lesson A Objectives After studying Lesson A, you should be able to: Code the repetition structure using the Do...Loop statement Include the repetition structure in pseudocode Include the repetition structure in a flowchart Initialize and update counters and accumulators Code the repetition structure using the For...Next statement Programming with Microsoft Visual Basic 2008, Fourth Edition4

5 The Repetition Structure Repetition structure (or loop): Repeatedly processes instructions until condition is met –Example: Calculate net pay for each employee Pretest loop: Evaluates condition prior to processing instructions Posttest loop: Evaluates condition after processing instructions 5Programming with Microsoft Visual Basic 2008, Fourth Edition

6 The Do…Loop Statement Do…Loop statement: Used to code both pretest loop and posttest loop Two variations of syntax: –One for pretest loop and one for posttest loop –While keyword: Indicates that instructions should be processed while condition is true –Until keyword: Indicates that instructions should be processed until condition becomes true 6Programming with Microsoft Visual Basic 2008, Fourth Edition

7 The Do…Loop Statement (continued) Do…Loop statement: –Begins with Do clause, ends with Loop clause –Instructions to be repeated are placed between Do and Loop clauses –Use While or Until keyword before condition –Condition must evaluate to Boolean True or False Location of {While|Until} condition: –Pretest loop: Appears in Do clause –Posttest loop: Appears in Loop clause Diamond: Represents loop condition in flowchart 7Programming with Microsoft Visual Basic 2008, Fourth Edition

8 Figure 6-2: Syntax versions and examples of the Do...Loop statement 8Programming with Microsoft Visual Basic 2008, Fourth Edition The Do…Loop Statement (continued)

9 Figure 6-3: Processing steps for the pretest loop example from Figure 6-2 9Programming with Microsoft Visual Basic 2008, Fourth Edition The Do…Loop Statement (continued)

10 Programming with Microsoft Visual Basic 2008, Fourth Edition10 Figure 6-4: Processing steps for the posttest loop example from Figure 6-2 The Do…Loop Statement (continued)

11 Figure 6-5: Pseudocode and flowchart for the pretest loop example shown in Figure Programming with Microsoft Visual Basic 2008, Fourth Edition The Do…Loop Statement (continued)

12 Programming with Microsoft Visual Basic 2008, Fourth Edition12 Figure 6-6: Pseudocode and flowchart for the posttest loop example shown in Figure 6-2 The Do…Loop Statement (continued)

13 Counters and Accumulators Used to calculate subtotals, totals, and averages Counter: Numeric variable used for counting Accumulator: Variable used to tally various amounts Initializing: Assigning initial value to counter or accumulator Updating (incrementing or decrementing): –Changing value stored in counter or accumulator –Update statement must be within repetition structure 13Programming with Microsoft Visual Basic 2008, Fourth Edition

14 The Sales Express Application Objective: Display average annual sales of company Application uses pretest loop Priming read: Use to prime (prepare or set up) loop by performing first action prior to entering loop 14Programming with Microsoft Visual Basic 2008, Fourth Edition

15 The Sales Express Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition15 Figure 6-7: Pseudocode for the bntCalc control’s Click event procedure

16 The For…Next Statement For…Next statement: –Processes instructions specific number of times –Condition tested before processing (pretest loop) –Also called counter controlled loop Must specify start value, end value, and step value –Start value and end value provide looping range –Step value increments or decrements counter Hexagon: Flowchart symbol 16Programming with Microsoft Visual Basic 2008, Fourth Edition

17 Figure 6-11: Pseudocode and flowchart for Example 1 in Figure Programming with Microsoft Visual Basic 2008, Fourth Edition The For…Next Statement (continued)

18 The Monthly Payment Calculator Application Task of btnCalc control’s Click event procedure: –Calculate and display monthly car payments –Use term of five years and rates from 5 – 10% Basic structure of For…Next statement –Use procedure level variable, dblRate, as counter –Set starting and ending values to 0.05 and 0.1 –Set step value to 0.01 –Calculate monthly payment for current rate –Display interest rate and corresponding payment 18Programming with Microsoft Visual Basic 2008, Fourth Edition

19 The Monthly Payment Calculator Application (continued) Figure 6-11: Additional code entered in the btnCalc control’s Click event procedure 19Programming with Microsoft Visual Basic 2008, Fourth Edition

20 The Monthly Payment Calculator Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition20 Figure 6-12: Monthly payments shown in the interface

21 Lesson A Summary Repetition structure (loop): Repeats set of instructions until some condition is met Use Do...Loop statement to code pretest and posttest loops Counters are used to track count of something Accumulators are used to tally up an amount Counters and accumulators must be: –Initialized –Updated within loop 21Programming with Microsoft Visual Basic 2008, Fourth Edition

22 Lesson A Summary (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition22 Use For...Next statement to code pretest loops Use hexagon to show a For…Next loop in flowchart

23 Lesson B Objectives After studying Lesson B, you should be able to: Nest repetition structures Refresh the screen Delay program execution 23Programming with Microsoft Visual Basic 2008, Fourth Edition

24 Nested Repetition Structures Nested repetition structure: –Inner loop placed entirely within outer loop –Inner loop is referred to as nested loop Clocks use nested loops to keep track of time Minute and hour hands of clock can be compared to loops: –Outer loop corresponds to hour hand –Inner loop corresponds to minute hand 24Programming with Microsoft Visual Basic 2008, Fourth Edition

25 Nested Repetition Structures (continued) Figure 6-16: Logic used by a clock’s hour and minute hands 25Programming with Microsoft Visual Basic 2008, Fourth Edition

26 The Refresh and Sleep Methods Refresh method: Ensures that any code appearing before it that affects interface’s appearance is processed Syntax : Me.Refresh() – Me refers to current form Sleep method: Delays program execution Syntax: System.Threading.Thread.Sleep(milliseconds) –Millisecond: 1/1000 of second 26Programming with Microsoft Visual Basic 2008, Fourth Edition

27 The Refresh and Sleep Methods (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition27 Figure 6-18: Refresh and Sleep methods added to the procedure

28 Monthly Payment Calculator Application—Nested For...Next Statements Objective: Calculate and display car payments Use nested loops in btnCalc control’s Click event Outer For…Next statement: –Control interest rates ranging from % –Increment rates at each iteration by 1% Inner For…Next statement: –Controls terms from years 28Programming with Microsoft Visual Basic 2008, Fourth Edition

29 Monthly Payment Calculator Application—Nested For...Next Statements (continued) Figure 6-19: Outer and inner (nested) loops entered in the procedure 29Programming with Microsoft Visual Basic 2008, Fourth Edition

30 Monthly Payment Calculator Application—Nested For...Next Statements (continued) Figure 6-20: Monthly payments shown in the interface 30Programming with Microsoft Visual Basic 2008, Fourth Edition

31 Lesson B Summary To nest repetition structure, place entire inner loop within outer loop To refresh interface, use Refresh method To pause program execution, use Sleep method 31Programming with Microsoft Visual Basic 2008, Fourth Edition

32 Lesson C Objectives After studying Lesson C, you should be able to: Include a list box in an interface Select a list box item from code Determine the selected item in a list box 32Programming with Microsoft Visual Basic 2008, Fourth Edition

33 Coding the Shoppers Haven Application Objective: Allow entry of price and discount rate Application requirements: –Discount rates range from 10% through 30% –Discount rate should be incremented by 5% –Calculate discount amount and discounted price –Display discount amount and discounted price 33Programming with Microsoft Visual Basic 2008, Fourth Edition

34 Coding the Shoppers Haven Application (continued) Figure 6-23: TOE chart for the Shoppers Haven application 34Programming with Microsoft Visual Basic 2008, Fourth Edition

35 Coding the Shoppers Haven Application (continued) Figure 6-24: Partially completed user interface for the Shoppers Haven application 35Programming with Microsoft Visual Basic 2008, Fourth Edition

36 Including a List Box in an Interface List box: Displays list of choices from which 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 list box to interface List box can be made any size you want 36Programming with Microsoft Visual Basic 2008, Fourth Edition

37 Adding Items to a List Box Collection: Group of objects treated as one unit Items collection: Refers to group of items in list box Index: Unique number that identifies each item in collection –Is zero-relative (starts with 0) Items collection’s Add method: Used to add item to list box –Implemented in form’s Load event procedure to prepare list box before form is displayed 37Programming with Microsoft Visual Basic 2008, Fourth Edition

38 Adding Items to a List Box (continued) Figure 6-26: Syntax and examples of the Add method 38Programming with Microsoft Visual Basic 2008, Fourth Edition

39 Adding Items to a List Box (continued) Figure 6-27: Result of processing the code shown in Figure Programming with Microsoft Visual Basic 2008, Fourth Edition

40 Adding Items to a List Box (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition40 Sorted property of list box: Determines order in which items are displayed in list box –If true, newly added item is placed in its proper position –If false, newly added item is placed at end of list –Uses dictionary sort order

41 The SelectedItem And SelectedIndex Properties SelectedItem property: Contains value of item selected in list box –Contains empty string when no item is selected SelectedIndex property: Contains index of item selected in list box –Contains -1 when no item is selected Default list box item: Appears when application is first loaded –Can be chosen by setting either SelectedItem or SelectedIndex property 41Programming with Microsoft Visual Basic 2008, Fourth Edition

42 The SelectedItem And SelectedIndex Properties (continued) Figure 6-30: Examples of the SelectedItem and SelectedIndex properties 42Programming with Microsoft Visual Basic 2008, Fourth Edition

43 The SelectedItem And SelectedIndex Properties (continued) Figure 6-31: Default item selected in the list box 43Programming with Microsoft Visual Basic 2008, Fourth Edition

44 The SelectedValueChanged and SelectedIndexChanged Events Programming with Microsoft Visual Basic 2008, Fourth Edition44 SelectedValueChanged and SelectedIndexChanged events: Occur when user or code statement selects item in list box –Can use these events to process instructions when selection is made

45 Coding the btnCalc Control’s Click Event Procedure 45Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 6-32: Pseudocode for the btnCalc control’s Click event procedure

46 Coding the btnCalc Control’s Click Event Procedure (continued) Figure 6-33: Discount and discounted price shown in the interface 46Programming with Microsoft Visual Basic 2008, Fourth Edition

47 Lesson C Summary A list box displays list of items Use SelectionMode property to set number of items that can be selected in list box Use Items.Add method to add items to list box Use Sorted property to sort list box items in dictionary order Use SelectedItem or SelectedIndex property to determine which item was selected 47Programming with Microsoft Visual Basic 2008, Fourth Edition

48 Lesson C Summary (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition48 Use SelectedValueChanged or SelectedIndexChanged events to perform tasks when an item in list box is selected


Download ppt "Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Six The Repetition Structure."

Similar presentations


Ads by Google