Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic th Edition

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic th Edition"— Presentation transcript:

1 Programming with Microsoft Visual Basic 2010 5th Edition
Chapter Six The Repetition Structure

2 Previewing the Shopper’s Haven Application
Open the Shoppers.exe file The Shopper’s 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 2010, 5th Edition

3 Previewing the Shopper’s Haven Application (cont’d.)
Figure 6-1 Discount and discounted price shown in the interface Programming with Microsoft Visual Basic 2010, 5th Edition

4 Lesson A Objectives After studying Lesson A, you should be able to:
Differentiate between a looping condition and a loop exit condition Explain the difference between a pretest loop and a posttest loop Include pretest and posttest loops in pseudocode and a flowchart Write a Do…Loop statement

5 Lesson A Objectives (cont’d.)
Stop an infinite loop Utilize counters and accumulators Explain the purpose of the priming and update reads Abbreviate assignment statements using the arithmetic assignment operators Code a counter-controlled loop using the For…Next statement

6 Repeating Program Instructions
Repetition structure (or loop) Repeatedly processes instructions until condition is met Example: “Make hay while the sun shines” Looping condition The requirement for repeating the instructions In above example: “while the sun shines” Loop exit condition The requirement for not repeating the instructions Example: “until the sun stops shining”

7 Repeating Program Instructions (cont’d.)
Pretest loop Evaluates condition prior to processing instructions Posttest loop Evaluates condition after processing instructions

8 The Do…Loop Statement Do…Loop statement Two variations of syntax
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

9 The Do…Loop Statement (cont’d.)
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

10 Figure 6-7 Syntax versions and examples of the Do...Loop statement

11 Figure 6-8 Processing steps for the loop examples from Figure 6-7 (continues)

12 Figure 6-8 Processing steps for the loop examples from Figure 6-7 (cont’d.)

13 Figure 6-9 Flowcharts for the loop examples from Figure 6-7

14 The Do…Loop Statement (cont’d.)
Infinite loop Also called endless loop The condition to end the loop is never met To stop an infinite loop: Click Debug on the menu bar Then click Stop Debugging

15 Counters and Accumulators
Used to calculate subtotals, totals, and averages Counter: Numeric variable used for counting Accumulator: Numeric variable for accumulating (adding together) something Initializing Assigning beginning value Updating (incrementing) Adding a number to accumulator’s value Number can be positive or negative Done within the loop body Programming with Microsoft Visual Basic 2010, 5th Edition

16 The Sales Express Company Application
Objective: Calculate average sales amount entered by sales manager Application uses pretest loop Priming read Use to prime (prepare or set up) loop by performing first action prior to entering loop Update read Allows user to update value that controls loop’s condition Programming with Microsoft Visual Basic 2010, 5th Edition

17 Figure 6-16 Pseudocode for the Average button’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5th Edition

18 Arithmetic Assignment Operators
Used to abbreviate an assignment statement: Containing an arithmetic operator Format variableName = variableName arithmeticOperator value Example intAge = intAge + 1 can be abbreviated intAge += 1

19 Figure 6-20 Syntax and examples of the arithmetic assignment operators (continues)

20 Figure 6-20 Syntax and examples of the arithmetic assignment operators (cont’d.)

21 The For…Next Statement
Used to code a counter-controlled loop Processes instructions precise number of times Condition tested before processing (pretest loop) Must specify start value, end value, and step value Start value and end value provide looping range Step value increments or decrements counter For clause represented by a hexagon in flowchart

22 Figure 6-22 For…Next statement’s syntax, examples, and processing tasks (continues)

23 Figure 6-22 For…Next statement’s syntax, examples, and processing tasks (cont’d.)

24 Figure 6-23 Processing steps for Example 1 in Figure 6-22

25 The Monthly Payment Calculator Application
Figure 6-24 Problem specification for the Monthly Payment Calculator application Figure 6-25 Sample run of the Monthly Payment Calculator application Programming with Microsoft Visual Basic 2010, 5th Edition

26 The Monthly Payment Calculator Application (cont’d.)
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

27 The Monthly Payment Calculator Application (cont’d.)
Figure 6-26 Pseudocode and flowchart for the Calculate button’s Click event procedure (cont’d.)

28 Figure 6-26 Pseudocode and flowchart for the Calculate button’s Click event procedure

29 Comparing the For…Next and Do…Loop Statements
Either can be used to code counter-controlled loop For…Next is more convenient When using a Do…Loop statement: Must declare and initialize the counter variables And update the counter variable Include the appropriate comparison in the Do clause When using For…Next Declaration, initialization, update, and comparison Handled by the For clause

30 Lesson A Summary Repetition structure (loop): Repeats set of instructions until some condition is met To stop an infinite loop Click Debug on the menu bar and then click Stop Debugging Counters and accumulators must be: Initialized Updated within loop Arithmetic assignment operators Used to abbreviate an assignment statement

31 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 2010, 5th Edition

32 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 second hands of clock can be compared to loops Outer loop corresponds to minute hand Inner loop corresponds to second hand

33 Nested Repetition Structures (cont’d.)
Figure 6-31 Logic used by a clock’s minute and second hands

34 The Refresh and Sleep Methods
Refresh method: Ensures that computer processes any previous lines of code that affect interface appearance Syntax: Me.Refresh() Me refers to current form Sleep method: Delays program execution Syntax: System.Threading.Thread.Sleep(millisecond s) Millisecond: 1/1000 of second

35 The Refresh and Sleep Methods (cont’d.)
Figure 6-33 Refresh and Sleep methods added to the procedure Programming with Microsoft Visual Basic 2010, 5th Edition

36 Revisiting the Monthly Payment Calculator Application
Objective: Calculate and display car payments Use nested loops in btnCalc control’s Click event procedure Outer loop Control interest rates ranging from % Increment rates at each iteration by 1% Inner loop Controls terms from years

37 Figure 6-35 Outer and inner nested loops entered in the procedure
Figure 6-34 Modified problem specification for the Monthly Payment Calculator application Figure 6-35 Outer and inner nested loops entered in the procedure Programming with Microsoft Visual Basic 2010, 5th Edition

38 Figure 6-36 Monthly payments shown in the interface
Programming with Microsoft Visual Basic 2010, 5th Edition

39 Lesson B Summary To nest repetition structure: To refresh interface:
Place entire inner loop within outer loop To refresh interface: Use Refresh method Syntax: Me.Refresh() To pause program execution: Use Sleep method Syntax: System.Threading.Thread.Sleep(milliseconds)

40 Lesson C Objectives After studying Lesson C, you should be able to:
Include a list box on a form Select a list box item from code Determine the selected item in a list box

41 Creating the Shopper’s Haven Application
Application requirements User enters price and discount rate 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

42 Figure 6-39 TOE chart for the Shopper’s Haven application
Programming with Microsoft Visual Basic 2010, 5th Edition

43 Including a List Box in an Interface
Displays list of choices from which user can select zero or more choices SelectionMode property Controls number of choices that can be selected ListBox tool Used to add list box to interface List box can be made any size you want Windows standard: Three to eight choices

44 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 First item has index of zero Items collection’s Add method Used to add item to list box Usually entered in form’s Load event procedure

45 Figure 6-41 Syntax and examples of the Items collection’s Add method
Programming with Microsoft Visual Basic 2010, 5th Edition

46 Adding Items to a List Box (cont’d.)
Figure 6-42 Result of processing the code from Figure 6-41 Programming with Microsoft Visual Basic 2010, 5th Edition

47 Adding Items to a List Box (cont’d.)
Sorted property of list box Determines order of displayed items 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

48 Coding the Shopper’s Haven Application
Enter the appropriate Add methods: In the form’s Load event procedure Follow instructions on page 378

49 The SelectedItem and SelectedIndex Properties
SelectedItem property Contains value of selected item Contains empty string when no item is selected SelectedIndex property Contains value of selected item’s index Contains -1 when no item is selected Default list box item Appears when application is first loaded Chosen by setting either SelectedItem or SelectedIndex property

50 The SelectedItem and SelectedIndex Properties (cont’d.)
Figure 6-45 Second item selected in the list box

51 Figure 6-46 Examples of the list box’s SelectedItem and SelectedIndex properties
Programming with Microsoft Visual Basic 2010, 5th Edition

52 Figure 6-47 Examples of selecting the default list box item
Programming with Microsoft Visual Basic 2010, 5th Edition

53 Figure 6-48 Default item selected in the list box
Programming with Microsoft Visual Basic 2010, 5th Edition

54 The SelectedValueChanged and SelectedIndexChanged Events
SelectedValueChanged event and SelectedIndexChanged event 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 2010, 5th Edition

55 Coding the btnCalc Control’s Click Event Procedure
Figure 6-49 Pseudocode for the btnCalc control’s Click event procedure

56 Coding the btnCalc Control’s Click Event Procedure (cont’d.)
Figure 6-50 Calculated amounts shown in the interface Programming with Microsoft Visual Basic 2010, 5th Edition

57 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

58 Lesson C Summary (cont’d.)
To perform tasks when a different item is selected in a list box: Add code to SelectedValueChanged or SelectedIndexChanged procedures


Download ppt "Programming with Microsoft Visual Basic th Edition"

Similar presentations


Ads by Google