Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.

Similar presentations


Presentation on theme: "Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions."— Presentation transcript:

1 Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions

2 Microsoft Visual Basic 2010: Reloaded, Fourth Edition2 Objectives After studying this chapter, you should be able to: Differentiate between a pretest and a posttest loop Include pretest and posttest loops in pseudocode and in a flowchart Write a Do…Loop statement Utilize counters and accumulators Display a dialog box using the InputBox function

3 Microsoft Visual Basic 2010: Reloaded, Fourth Edition3 Objectives (cont'd.) Include a list box in an interface Enable and disable a control Refresh the screen Delay program execution

4 Microsoft Visual Basic 2010: Reloaded, Fourth Edition4 The Repetition Structure Repetition structure (or loop): a structure that repeatedly processes one or more program instructions until a condition is met Looping condition: the requirement for repeating the instructions Loop exit condition: the requirement for not repeating the instructions

5 The Repetition Structure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition5 Figure 6-1: Example of a looping condition and a loop exit condition

6 The Repetition Structure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition6 Figure 6-2: Problem specification for the Getting to a Million Club application Figure 6-3: Sample run of the Getting to a Million Club application

7 The Repetition Structure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition7 Figure 6-4: Pseudocode containing only the sequence structure

8 Microsoft Visual Basic 2010: Reloaded, Fourth Edition8 The Repetition Structure (cont’d.) Pretest loop –The condition is evaluated before the instructions within the loop are processed –The instructions may be processed zero or more times Posttest loop –The condition is evaluated after the instructions within the loop are processed –The instructions are always processed at least once

9 Microsoft Visual Basic 2010: Reloaded, Fourth Edition9 Figure 6-5: Modified problem specification and pseudocode containing a loop

10 Microsoft Visual Basic 2010: Reloaded, Fourth Edition10 The Repetition Structure (cont'd.) Repetition statements in Visual Basic –Do...Loop –For...Next –For Each...Next

11 Microsoft Visual Basic 2010: Reloaded, Fourth Edition11 The Do...Loop Statement Do...Loop statement: codes both a pretest and posttest loop Loop body: the instructions between the Do and the Loop clauses Use While or Until to code the condition for the loop Repetition symbol in a flowchart is the diamond

12 Microsoft Visual Basic 2010: Reloaded, Fourth Edition12 Figure 6-6: How to use the Do…Loop statement

13 Microsoft Visual Basic 2010: Reloaded, Fourth Edition13 The Do...Loop Statement (cont'd.) Figure 6-7: Flowchart for the pretest loop example from Figure 6-6

14 Microsoft Visual Basic 2010: Reloaded, Fourth Edition14 The Do...Loop Statement (cont'd.) Figure 6-8: Flowchart for the posttest loop example from Figure 6-6

15 Microsoft Visual Basic 2010: Reloaded, Fourth Edition15 Using Counters and Accumulators Counter: a numeric variable used for counting Accumulator: a numeric variable used for accumulating (adding together) Initializing: assigning a beginning value to a counter or accumulator variable Updating (or incrementing): adding a number to the value of a counter or accumulator variable Counters are always incremented by a constant value, usually 1

16 Microsoft Visual Basic 2010: Reloaded, Fourth Edition16 Figure 6-9: Modified pseudocode and code for the calcButton’s Click event procedure

17 Microsoft Visual Basic 2010: Reloaded, Fourth Edition17 Figure 6-9: Modified pseudocode and code for the calcButton’s Click event procedure (cont’d.) Using Counters and Accumulators (cont’d.) Figure 6-10: Sample run of the modified Getting to a Million Club application

18 Microsoft Visual Basic 2010: Reloaded, Fourth Edition18 The Sales Express Company Application Requirements: display the average amount the company sold during the prior year Input: the amount of each salesperson’s sales Multiline property: if True, allows multiple lines of text in a text box ReadOnly property: if True, prevents a user from changing the text box contents during run time ScrollBars property: specifies whether a text box has no scroll bars, horizontal or vertical, or both

19 Microsoft Visual Basic 2010: Reloaded, Fourth Edition19 The Sales Express Company Application (cont'd.) Figure 6-11: Problem specification for the Sales Express Company application

20 Microsoft Visual Basic 2010: Reloaded, Fourth Edition20 The Sales Express Company Application (cont'd.) Figure 6-12: Sample run of the Sales Express Company application

21 Microsoft Visual Basic 2010: Reloaded, Fourth Edition21 Figure 6-13: Pseudocode for the Average button’s Click event procedure The Sales Express Company Application (cont'd.)

22 Priming read: used to obtain the first input Update read: allows the user to update the value of an input item Infinite (or endless) loop: a loop that has no way to end Must verify that a variable does not contain the value 0 before using it as a divisor Microsoft Visual Basic 2010: Reloaded, Fourth Edition22

23 Microsoft Visual Basic 2010: Reloaded, Fourth Edition23 Figure 6-14: Flowchart for the Average button’s Click event procedure

24 Microsoft Visual Basic 2010: Reloaded, Fourth Edition24 The InputBox Function InputBox function: displays a predefined dialog box that allows the user to enter data –Contains a text message, an OK button, a Cancel button, and an input area InputBox function returns: –The user’s entry if the user clicks the OK button –An empty string if the user clicks the Cancel button or the Close button on the title bar

25 Microsoft Visual Basic 2010: Reloaded, Fourth Edition25 The InputBox Function (cont'd.) Figure 6-15: Example of an input dialog box

26 Microsoft Visual Basic 2010: Reloaded, Fourth Edition26 The InputBox Function (cont'd.) InputBox function arguments: –prompt: the message to display inside the dialog box –title: the text to display in the dialog box’s title bar –defaultResponse: a prefilled value for the user input

27 Microsoft Visual Basic 2010: Reloaded, Fourth Edition27 Figure 6-16: How to use the InputBox function

28 Microsoft Visual Basic 2010: Reloaded, Fourth Edition28 The InputBox Function (cont'd.) Figure 6-16: How to use the InputBox function (cont'd.)

29 Microsoft Visual Basic 2010: Reloaded, Fourth Edition29 Figure 6-17: Code associated with the pseudocode and flowchart shown in Figures 6-13 and 6-14, respectively

30 Microsoft Visual Basic 2010: Reloaded, Fourth Edition30 Figure 6-17: Code associated with the pseudocode and flowchart shown in Figures 6-13 and 6-14, respectively (cont’d.) The InputBox Function (cont'd.)

31 Microsoft Visual Basic 2010: Reloaded, Fourth Edition31 Including a List Box in an Interface List box: displays a list of choices from which the user can select zero or more choices SelectionMode property: controls the number of choices a user can select –None: user can scroll but not select anything –One: user can select one item –MultiSimple and MultiExtended: user can select multiple items

32 Microsoft Visual Basic 2010: Reloaded, Fourth Edition32 Adding Items to a List Box Items collection: a collection of the items in a list box Collection: a group of one or more individual objects treated as one unit Add method: adds an item to the list box’s Items collection –Items to be added must be converted to String –String Collection Editor window can be used to specify list items during design time Load event of a form: occurs when an application is started and the form is displayed for the first time

33 Microsoft Visual Basic 2010: Reloaded, Fourth Edition33 Adding Items to a List Box (cont'd.) Figure 6-18: String Collection Editor window

34 Microsoft Visual Basic 2010: Reloaded, Fourth Edition34 Adding Items to a List Box (cont'd.) Figure 6-19: How to use the Items collection’s Add method

35 Microsoft Visual Basic 2010: Reloaded, Fourth Edition35 Adding Items to a List Box (cont'd.) Figure 6-20: Add methods entered in the MainForm’s Load event procedure

36 Microsoft Visual Basic 2010: Reloaded, Fourth Edition36 Adding Items to a List Box (cont'd.) Figure 6-21: Result of processing the Add methods in Figure 6-20

37 Microsoft Visual Basic 2010: Reloaded, Fourth Edition37 Adding Items to a List Box (cont'd.) Figure 6-22: Sample run of the Jasper’s Food Hut application Figure 6-23: Add to List button’s Click event procedure in the Jasper’s Food Hut application

38 Microsoft Visual Basic 2010: Reloaded, Fourth Edition38 Adding Items to a List Box (cont'd.) Figure 6-24: Sample run of the Clark’s Chicken application Figure 6-25: Add to List button’s Click event procedure in the Clark’s Chicken application

39 Microsoft Visual Basic 2010: Reloaded, Fourth Edition39 Adding Items to a List Box (cont'd.) Sorted property: –Determines if the list box items are sorted –Sort order is dictionary order Figure 6-26: Examples of the list box’s Sorted property

40 Accessing Items in a List Box Index: –A unique number that identifies an item in a collection –Is zero-relative: the first item has index of 0 Microsoft Visual Basic 2010: Reloaded, Fourth Edition40

41 Accessing Items in a List Box (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition41 Figure 6-27: How to access an item in a list box

42 Determining the Number of Items in a List Box Items.Count property: stores the number of items in a list box –Count value is always one higher than the highest index in the list box Microsoft Visual Basic 2010: Reloaded, Fourth Edition42

43 Microsoft Visual Basic 2010: Reloaded, Fourth Edition43 Figure 6-28: How to determine the number of items in a list box

44 Microsoft Visual Basic 2010: Reloaded, Fourth Edition44 The SelectedItem and SelectedIndex Properties SelectedItem property: –Contains the value of the selected item in the list –If nothing is selected, it contains the empty string SelectedIndex property: –Contains the index of the selected item in the list –If nothing is selected, it contains the value -1 Default list box item: the item that is selected by default when the interface first appears

45 Microsoft Visual Basic 2010: Reloaded, Fourth Edition45 The SelectedItem and SelectedIndex Properties (cont'd.) Figure 6-29: Item selected in the animalListBox

46 Microsoft Visual Basic 2010: Reloaded, Fourth Edition46 Figure 6-30: How to use the SelectedItem and SelectedIndex properties

47 Microsoft Visual Basic 2010: Reloaded, Fourth Edition47 The SelectedItem and SelectedIndex Properties (cont'd.) Figure 6-31: How to select the default list box item

48 Microsoft Visual Basic 2010: Reloaded, Fourth Edition48 The SelectedValueChanged and SelectedIndexChanged Events SelectedValueChanged and SelectedIndexChanged events: –Occur when a user selects an item in a list box –Occur when a code statement selects an item in a list box

49 Microsoft Visual Basic 2010: Reloaded, Fourth Edition49 Figure 6-32: Code showing the SelectedValueChanged and SelectedIndexChanged event procedures The SelectedValueChanged and SelectedIndexChanged Events (cont’d.)

50 Microsoft Visual Basic 2010: Reloaded, Fourth Edition50 Figure 6-32: Code showing the SelectedValueChanged and SelectedIndexChanged event procedures (cont’d.) The SelectedValueChanged and SelectedIndexChanged Events (cont’d.)

51 Microsoft Visual Basic 2010: Reloaded, Fourth Edition51 Figure 6-32: Code showing the SelectedValueChanged and SelectedIndexChanged event procedures (cont’d.) The SelectedValueChanged and SelectedIndexChanged Events (cont’d.)

52 Microsoft Visual Basic 2010: Reloaded, Fourth Edition52 Figure 6-33: Result of processing the code shown in Figure 6-32 The SelectedValueChanged and SelectedIndexChanged Events (cont’d.)

53 Microsoft Visual Basic 2010: Reloaded, Fourth Edition53 The Product Finder Application Allows the user to enter a product ID Searches for the ID in a list box If found, highlights the ID If not found, ensures that no ID is highlighted in the list box

54 Microsoft Visual Basic 2010: Reloaded, Fourth Edition54 Figure 6-34: Sample run of the application when the produce ID is found Figure 6-35: Sample run of the application when the product ID is not found

55 Microsoft Visual Basic 2010: Reloaded, Fourth Edition55 The Product Finder Application (cont'd.) Figure 6-36: Pseudocode for the Find button’s Click event procedure

56 Microsoft Visual Basic 2010: Reloaded, Fourth Edition56 Figure 6-37: Product Finder application’s code

57 Microsoft Visual Basic 2010: Reloaded, Fourth Edition57 Figure 6-37: Product Finder application’s code (cont’d.)

58 The Color Viewer Application Enabled property: used to enable or disable a control –When False, the control appears dimmed (grayed out), indicating it is not available for use Refresh method: ensures that the computer processes any previous lines of code that affect the interface’s appearance Sleep method: delays program execution –Argument is specified in milliseconds Microsoft Visual Basic 2010: Reloaded, Fourth Edition58

59 The Color Viewer Application (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition59 Figure 6-38: MainForm in the Color Viewer application

60 The Color Viewer Application (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition60 Figure 6-39: View Colors button’s Click event procedure

61 Programming Tutorial 1 Microsoft Visual Basic 2010: Reloaded, Fourth Edition61 Figure 6-41: MainForm for the Roll ‘Em Game application Creating the Roll ‘Em Game

62 Microsoft Visual Basic 2010: Reloaded, Fourth Edition62 Programming Tutorial 2 Figure 6-52: MainForm for the Just Birthdays application Coding the Just Birthdays Application

63 Microsoft Visual Basic 2010: Reloaded, Fourth Edition63 Programming Example Grade Calculator Application Figure 6-63: MainForm in the Grade Calculator application

64 Microsoft Visual Basic 2010: Reloaded, Fourth Edition64 Summary The three programming structures are sequence, selection, and repetition Repetition structure (or loop): repeatedly processes a set of instructions Pretest loop tests the condition before the instructions are processed Posttest loop tests the condition after the instructions are processed Do...Loop statement: codes a pretest or posttest loop

65 Microsoft Visual Basic 2010: Reloaded, Fourth Edition65 Summary (cont'd.) Use a While or Until condition in a Do...Loop Flowchart symbol for repetition is a diamond Counter and accumulators: variables that calculate subtotals, totals, and averages Priming read gets the first value prior to the loop Update read gets remaining values from the user InputBox function: allows user input Verify that a variable does not contain a value of 0 before using it as a divisor List box: displays a list of choices

66 Microsoft Visual Basic 2010: Reloaded, Fourth Edition66 Summary (cont'd.) List box’s Items collection: contains the items in the list box Use String Collection Editor window to add list items during design time Items.Add method: adds an item to the list during run time Form’s Load event occurs before the form appears List box item’s index is used to access the item Items.Count property stores the number of items

67 Microsoft Visual Basic 2010: Reloaded, Fourth Edition67 Summary (cont'd.) SelectedItem property of a list box: contains the value of the selected item in the list SelectedIndex property of a list box: contains the index position of the selected item in the list SelectedValueChanged and SelectedIndexChanged events occur when an item in a list box is selected Enabled property: enables or disables a control Sleep method: delays program execution Me.Refresh : refreshes (redraws) the form


Download ppt "Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions."

Similar presentations


Ads by Google