470 likes | 924 Vues
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
E N D
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 • 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) 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: 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: 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: Sample run of the application that contains the procedure Programming with Microsoft Visual Basic 2005, Third Edition
The Do…Loop Statement • Do…Loop statement • 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: 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: 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: 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 StructuresLesson 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
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 ApplicationLesson 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 • List box • 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 • 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