Chapter 6: The Repetition Structure

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

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.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
Programming with Microsoft Visual Basic 2005, Third Edition
An Introduction to Programming with C++ Fifth Edition
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
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.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
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
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
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.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
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
Clearly Visual Basic: Programming with Visual Basic nd Edition
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
Introduction to Problem Solving and Control Statements
Presentation transcript:

Chapter 6: The Repetition Structure Programming with Microsoft Visual Basic 2005, Third Edition

The Repetition Structure (Looping) Lesson A Objectives Code the repetition structure using the For...Next and Do...Loop statements Include the repetition structure in pseudocode Include the repetition structure in a flowchart Initialize and update counters and accumulators Programming with Microsoft Visual Basic 2005, Third Edition

Previewing the Completed Application Go to Run command on Windows Start menu Browse to the VB2005\Chap06 folder Open the Shoppers.exe file The Shoppers Haven user interface appears Programming with Microsoft Visual Basic 2005, Third Edition

Previewing the Completed Application (continued) Figure 6-1: Interface showing the discount and discounted price amounts Programming with Microsoft Visual Basic 2005, Third Edition

The Repetition Structure Repetition structure (loop) Repeatedly processes instructions until condition met Example: calculate net pay for each employee Pretest loop Condition evaluated prior to instruction processing Posttest loop Condition evaluated after instruction processing Programming with Microsoft Visual Basic 2005, Third Edition

The For…Next Statement Processes instructions a specific number of times Condition tested before processing (pretest loop) Also called a counter controlled loop Syntax and examples to follow Programming with Microsoft Visual Basic 2005, Third Edition

The For…Next Statement (continued) Figure 6-2: Syntax and examples of the For...Next statement (continued) Page 485 Programming with Microsoft Visual Basic 2005, Third Edition

The For…Next Statement (continued) Figure 6-2: Syntax and examples of the For...Next statement Programming with Microsoft Visual Basic 2005, Third Edition

The For…Next Statement (continued) Syntactic Elements of For…Next statement Begins with the For clause, ends with Next clause counter: numeric variable tracking iterations startvalue and endvalue provide looping range stepvalue increments or decrements counter statements: processed in the body of the loop Hexagon: flowchart symbol representing For…Next Programming with Microsoft Visual Basic 2005, Third Edition

The For…Next Statement (continued) Figure 6-5: Page 488, Pseudocode for the first example shown in Figure 6-2 Programming with Microsoft Visual Basic 2005, Third Edition

The For…Next Statement (continued) Figure 6-6: Page 489: Flowchart for the first example shown in Figure 6-2 Programming with Microsoft Visual Basic 2005, Third Edition

The Monthly Payment Calculator Application Task of xCalcButton’s Click event procedure Calculate and display monthly car payments Use term of five years and rates from 5 – 10% Basic structure of the For…Next statement Use a procedure level variable, rate, as a counter Set starting and ending values to 0.05 and 0.01 Set the step value to 0.1 Calculate monthly payment for current rate Display interest rate and corresponding payment Programming with Microsoft Visual Basic 2005, Third Edition

The Monthly Payment Calculator Application (continued) Figure 6-8: Page 491: Sample run of the application that contains the procedure Programming with Microsoft Visual Basic 2005, Third Edition

The Do…Loop Statement Do…Loop statement Syntax and examples to follow Codes both a pretest loop and a posttest loop Syntax and examples to follow Observe two variations of the syntax Variations correspond to pretest and posttest loop Programming with Microsoft Visual Basic 2005, Third Edition

The Do…Loop Statement (continued) Figure 6-9: Page 493: Syntax and examples of the Do...Loop statement (continued) Programming with Microsoft Visual Basic 2005, Third Edition

The Do…Loop Statement (continued) Figure 6-9: Syntax and examples of the Do...Loop statement Programming with Microsoft Visual Basic 2005, Third Edition

The Do…Loop Statement (continued) Syntactic elements for the Do…Loop statement Begins with Do clause, ends with the Loop clause Enter instructions to repeat between both clauses Use either While or Until keyword before condition Condition must evaluate to Boolean True or False Location of {While|Until} condition by syntax version Pretest loop: appears in the Do clause Posttest loop: appears in the Loop clause Diamond: represents loop condition in a flowchart Programming with Microsoft Visual Basic 2005, Third Edition

The Do…Loop Statement (continued) Figure 6-13: Page 495: Pseudocode and flowchart for the posttest loop example shown in Figure 6-9 (continued) Programming with Microsoft Visual Basic 2005, Third Edition

The Do…Loop Statement (continued) Figure 6-13: Pseudocode and flowchart for the posttest loop example shown in Figure 6-9 Programming with Microsoft Visual Basic 2005, Third Edition

Using Counters and Accumulators Used to calculate subtotals, totals, and averages Counter: numeric variable used for counting Accumulator: variable used to tally various amounts Initialize: set initial value of counter or accumulator Updating (incrementing or decrementing) Changing value stored in counter or accumulator Update statement is used within a repetition structure Programming with Microsoft Visual Basic 2005, Third Edition

The Sales Express Application Objective: display average sales of company Structure of xCalcButton Click event procedure Use a pretest loop to retrieve each sales amount Update accumulator within loop to tally gross sales Update counter within loop to keep track of entries Exit loop after data entry has been completed If counter is > 0, calculate average sales amount Display the average sales amount Programming with Microsoft Visual Basic 2005, Third Edition

The Sales Express Application (continued) Figure 6-15: Page 500: Pseudocode for the xCalcButton’s Click event procedure Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson A Repetition structure (loop): repeats a set of instructions until some condition is met Use a For...Next statement to code pretest loops Use a Do...Loop statement to code pretest and posttest loops Counters and accumulators must be initialized Counters and accumulators are updated in a loop Programming with Microsoft Visual Basic 2005, Third Edition

Nested Repetition Structures Lesson B Objectives Nest repetition structures Programming with Microsoft Visual Basic 2005, Third Edition

Nesting Repetition Structures Nested repetition structure Inner loop placed entirely within outer loop Placement of inner loop is known as nesting Clocks use nested loops to keep track of the time Analogizing minute and second hands to loops Outer loop corresponds to the minute hands Inner loop corresponds to the second hand Programming with Microsoft Visual Basic 2005, Third Edition

Nesting Repetition Structures (continued) Figure 6-22: Nested loops used by a clock Programming with Microsoft Visual Basic 2005, Third Edition

Monthly Payment Calculator Application— Nested For...Next Statements Objective: calculate and display car payments Use nested loops in xCalcButton’s Click event Role of the outer For…Next statement Control interest rates ranging from 5 - 10% Increment rates at each iteration by 1% Role of the inner For…Next statement Controls terms from 3 - 5 years Programming with Microsoft Visual Basic 2005, Third Edition

Figure 6-24: Monthly payments shown in the interface Monthly Payment Calculator Application— Nested For...Next Statements (continued) Figure 6-24: Monthly payments shown in the interface Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson B To nest a repetition structure, place the entire inner loop within the outer loop Programming with Microsoft Visual Basic 2005, Third Edition

Coding the Shoppers Haven Application Lesson C Objectives 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 2005, Third Edition

Shoppers Haven Objective: allow entry of price and discount rate Application requirements Discount rate range: 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 2005, Third Edition

Shoppers Haven (continued) Figure 6-25: TOE chart for the Shoppers Haven application Programming with Microsoft Visual Basic 2005, Third Edition

Shoppers Haven (continued) Figure 6-26: Partially completed user interface for the Shoppers Haven application Programming with Microsoft Visual Basic 2005, Third Edition

Including a List Box in an Interface Displays a list of choices 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 a list box to an interface List box can be made any size you want Programming with Microsoft Visual Basic 2005, Third Edition

Adding Items to a List Box Collection: group of objects treated as one unit Items collection Refers to group of items in a list box An index identifies each item in a collection Items collection’s Add method: Specifies items you want displayed in a list box Implemented in form’s Load event procedure Sorted property of a list box Displays list box items in dictionary order when true Programming with Microsoft Visual Basic 2005, Third Edition

Adding Items to a List Box (continued) Figure 6-28: Syntax and examples of the Add method Programming with Microsoft Visual Basic 2005, Third Edition

Adding Items to a List Box (continued) Figure 6-29: Items added to the list boxes Programming with Microsoft Visual Basic 2005, Third Edition

The SelectedItem And SelectedIndex Properties SelectedItem property Contains value of item selected in a list box Value when no item is selected is the empty string SelectedIndex property Contains index of item selected in a list box Value when no item is selected is the number -1 Default list box item Appears when the application is first loaded Can be chosen with SelectedItem and SelectedIndex Programming with Microsoft Visual Basic 2005, Third Edition

The SelectedItem And SelectedIndex Properties (continued) Figure 6-34: Completed MainForm’s Load event procedure Programming with Microsoft Visual Basic 2005, Third Edition

The SelectedItem And SelectedIndex Properties (continued) Figure 6-35: First item selected in the xRateListBox Programming with Microsoft Visual Basic 2005, Third Edition

Coding the Text Box’s TextChanged Event Procedure Control’s TextChanged event Occurs when contents of Text property change Additional requirement Clear controls when TextChanged event occurs in xOrigPriceTextBox Programming with Microsoft Visual Basic 2005, Third Edition

Coding the Text Box’s TextChanged Event Procedure (continued) Figure 6-36: Completed ClearLabels procedure Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xCalcButton’s Click Event Procedure Last procedure to code in application Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xCalcButton’s Click Event Procedure (continued) Figure 6-37: Pseudocode for the xCalcButton’s Click event procedure Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xCalcButton’s Click Event Procedure (continued) Figure 6-39: Discount and discounted price amounts shown in the interface Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson C A list box displays a list of items Use list box’s SelectionMode property to set number of items to select in a list box List box items can be sorted in dictionary order Use Item collection’s Add method to specify items that will display in the list box Text box’s TextChanged event occurs when a change is made to the control’s contents Programming with Microsoft Visual Basic 2005, Third Edition