Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER SIX THE REPETITION STRUCTURE.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER SIX THE REPETITION STRUCTURE."— Presentation transcript:

1 Programming with Microsoft Visual Basic th 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 2

3 Previewing the Shopper’s Haven Application (cont’d.) PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 3 Figure 6-1 Discount and discounted price shown in the interface

4 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 4

5 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 5

6 Programming with Microsoft Visual Basic 2010, 5 th Edition 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” 6

7 Programming with Microsoft Visual Basic 2010, 5 th Edition Repeating Program Instructions (cont’d.) Pretest loop ◦Evaluates condition prior to processing instructions Posttest loop ◦Evaluates condition after processing instructions 7

8 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 8

9 Programming with Microsoft Visual Basic 2010, 5 th Edition The Do…Loop Statement (cont’d.) 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 9

10 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 6-7 Syntax versions and examples of the Do...Loop statement 10

11 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 6-8 Processing steps for the loop examples from Figure 6-7 (continues) 11

12 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 6-8 Processing steps for the loop examples from Figure 6-7 (cont’d.) 12

13 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 6-9 Flowcharts for the loop examples from Figure

14 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 14

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 15

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 16

17 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 17 Figure 6-16 Pseudocode for the Average button’s Click event procedure

18 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 18

19 Programming with Microsoft Visual Basic 2010, 5 th Edition 19 Figure 6-20 Syntax and examples of the arithmetic assignment operators (continues)

20 Programming with Microsoft Visual Basic 2010, 5 th Edition 20 Figure 6-20 Syntax and examples of the arithmetic assignment operators (cont’d.)

21 Programming with Microsoft Visual Basic 2010, 5 th Edition The For…Next Statement 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 21

22 Programming with Microsoft Visual Basic 2010, 5 th Edition 22 Figure 6-22 For…Next statement’s syntax, examples, and processing tasks (continues)

23 Programming with Microsoft Visual Basic 2010, 5 th Edition 23 Figure 6-22 For…Next statement’s syntax, examples, and processing tasks (cont’d.)

24 Programming with Microsoft Visual Basic 2010, 5 th Edition 24 Figure 6-23 Processing steps for Example 1 in Figure 6-22

25 The Monthly Payment Calculator Application PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 25 Figure 6-24 Problem specification for the Monthly Payment Calculator application Figure 6-25 Sample run of the Monthly Payment Calculator application

26 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 26

27 Programming with Microsoft Visual Basic 2010, 5 th Edition The Monthly Payment Calculator Application (cont’d.) 27 Figure 6-26 Pseudocode and flowchart for the Calculate button’s Click event procedure (cont’d.)

28 Programming with Microsoft Visual Basic 2010, 5 th Edition 28 Figure 6-26 Pseudocode and flowchart for the Calculate button’s Click event procedure

29 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 29

30 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 30

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 31

32 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 second hands of clock can be compared to loops ◦Outer loop corresponds to minute hand ◦Inner loop corresponds to second hand 32

33 Programming with Microsoft Visual Basic 2010, 5 th Edition Nested Repetition Structures (cont’d.) 33 Figure 6-31 Logic used by a clock’s minute and second hands

34 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 34

35 The Refresh and Sleep Methods (cont’d.) PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 35 Figure 6-33 Refresh and Sleep methods added to the procedure

36 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 36

37 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 37 Figure 6-34 Modified problem specification for the Monthly Payment Calculator application Figure 6-35 Outer and inner nested loops entered in the procedure

38 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 38 Figure 6-36 Monthly payments shown in the interface

39 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary To nest repetition structure: ◦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) 39

40 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 40

41 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 41

42 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 42 Figure 6-39 TOE chart for the Shopper’s Haven application

43 Programming with Microsoft Visual Basic 2010, 5 th Edition Including a List Box in an Interface List box ◦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 43

44 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 44

45 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 45 Figure 6-41 Syntax and examples of the Items collection’s Add method

46 Adding Items to a List Box (cont’d.) PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 46 Figure 6-42 Result of processing the code from Figure 6-41

47 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 47

48 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Shopper’s Haven Application Enter the appropriate Add methods: ◦In the form’s Load event procedure Follow instructions on page

49 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 49

50 Programming with Microsoft Visual Basic 2010, 5 th Edition The SelectedItem and SelectedIndex Properties (cont’d.) 50 Figure 6-45 Second item selected in the list box

51 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 51 Figure 6-46 Examples of the list box’s SelectedItem and SelectedIndex properties

52 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 52 Figure 6-47 Examples of selecting the default list box item

53 PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 53 Figure 6-48 Default item selected in the list box

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 54

55 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the btnCalc Control’s Click Event Procedure 55 Figure 6-49 Pseudocode for the btnCalc control’s Click event procedure

56 Coding the btnCalc Control’s Click Event Procedure (cont’d.) PROGRAMMING WITH MICROSOFT VISUAL BASIC 2010, 5TH EDITION 56 Figure 6-50 Calculated amounts shown in the interface

57 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 57

58 Programming with Microsoft Visual Basic 2010, 5 th Edition 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 58


Download ppt "Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER SIX THE REPETITION STRUCTURE."

Similar presentations


Ads by Google