Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2008 Fourth Edition

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2008 Fourth Edition"— 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 Programming 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 Programming 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 Edition

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 Programming 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

7 The Do…Loop Statement (continued)
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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

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

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

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

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 Programming 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

16 The 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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 Programming 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

24 Nested Repetition Structures
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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

34 Coding the Shoppers Haven Application (continued)
Figure 6-23: TOE chart for the Shoppers Haven application Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 6-26 Programming with Microsoft Visual Basic 2008, Fourth Edition

40 Adding Items to a List Box (continued)
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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

44 The SelectedValueChanged and SelectedIndexChanged Events
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 Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

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


Download ppt "Programming with Microsoft Visual Basic 2008 Fourth Edition"

Similar presentations


Ads by Google