Programming with Microsoft Visual Basic 2008 Fourth Edition

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Programming Logic and Design Eighth Edition
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Chapter 7: Sub and Function Procedures
1.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Objectives In this chapter, you will learn about:
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.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
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 Four The Selection Structure
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
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.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Programming Logic and Design Fifth Edition, Comprehensive
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
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.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
1.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
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.
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.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
© 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,
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 8: More on the Repetition Structure
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 4: The Selection Structure
Repeating Program Instructions
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Chapter 8: More on the Repetition Structure
Presentation transcript:

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Six The Repetition Structure

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 5 - 10% Increment rates at each iteration by 1% Inner For…Next statement: Controls terms from 3 - 5 years Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

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

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

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

Coding the Shoppers Haven Application (continued) Figure 6-23: TOE chart for the Shoppers Haven application Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

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

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

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

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

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

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

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

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

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

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

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

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