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

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Repeating Actions While and For Loops
An Introduction to Programming with C++ Fifth Edition
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Chapter 6: The Repetition Structure
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 5: More on the Selection Structure
1.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 16 I’m on the Inside; You’re on the Outside.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Controlling Program Flow with Looping Structures
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 14 Do It, Then Ask Permission.
Unit 6 Repetition Processing Instructor: Brent Presley.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Looping Structures. A B dialog box that pops up and prompts the user for input Text area that pops up and prompts the user for to wait while it gets.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
Microsoft Visual Basic 2008: Reloaded Third Edition
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.)

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

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

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

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

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

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

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

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

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.)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.)

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.)

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.)

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.)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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