Programming with Microsoft Visual Basic th Edition

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Programming Logic and Design Eighth Edition
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
An Introduction to Programming with C++ Fifth Edition
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 8: String Manipulation
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.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter Four The Selection Structure
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
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.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
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
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
© 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,
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
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
Microsoft Visual Basic 2005: Reloaded Second Edition
Objectives After studying this chapter, you should be able to:
CIS 16 Application Development Programming with Visual Basic
Chapter 8: More on the Repetition Structure
Introduction to Problem Solving and Control Statements
Presentation transcript:

Programming with Microsoft Visual Basic 2010 5th Edition Chapter Six The Repetition Structure

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

Previewing the Shopper’s Haven Application (cont’d.) Figure 6-1 Discount and discounted price shown in the interface Programming with Microsoft Visual Basic 2010, 5th 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

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

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”

Repeating Program Instructions (cont’d.) Pretest loop Evaluates condition prior to processing instructions Posttest loop Evaluates condition after processing instructions

The Do…Loop Statement Do…Loop statement Two variations of syntax 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

The Do…Loop Statement (cont’d.) 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

Figure 6-7 Syntax versions and examples of the Do...Loop statement

Figure 6-8 Processing steps for the loop examples from Figure 6-7 (continues)

Figure 6-8 Processing steps for the loop examples from Figure 6-7 (cont’d.)

Figure 6-9 Flowcharts for the loop examples from Figure 6-7

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

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

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

Figure 6-16 Pseudocode for the Average button’s Click event procedure Programming with Microsoft Visual Basic 2010, 5th 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

Figure 6-20 Syntax and examples of the arithmetic assignment operators (continues)

Figure 6-20 Syntax and examples of the arithmetic assignment operators (cont’d.)

The 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

Figure 6-22 For…Next statement’s syntax, examples, and processing tasks (continues)

Figure 6-22 For…Next statement’s syntax, examples, and processing tasks (cont’d.)

Figure 6-23 Processing steps for Example 1 in Figure 6-22

The Monthly Payment Calculator Application Figure 6-24 Problem specification for the Monthly Payment Calculator application Figure 6-25 Sample run of the Monthly Payment Calculator application Programming with Microsoft Visual Basic 2010, 5th 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

The Monthly Payment Calculator Application (cont’d.) Figure 6-26 Pseudocode and flowchart for the Calculate button’s Click event procedure (cont’d.)

Figure 6-26 Pseudocode and flowchart for the Calculate button’s Click event procedure

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

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

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

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 second hands of clock can be compared to loops Outer loop corresponds to minute hand Inner loop corresponds to second hand

Nested Repetition Structures (cont’d.) Figure 6-31 Logic used by a clock’s minute and second hands

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

The Refresh and Sleep Methods (cont’d.) Figure 6-33 Refresh and Sleep methods added to the procedure Programming with Microsoft Visual Basic 2010, 5th 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 5 - 10% Increment rates at each iteration by 1% Inner loop Controls terms from 3 - 5 years

Figure 6-35 Outer and inner nested loops entered in the procedure Figure 6-34 Modified problem specification for the Monthly Payment Calculator application Figure 6-35 Outer and inner nested loops entered in the procedure Programming with Microsoft Visual Basic 2010, 5th Edition

Figure 6-36 Monthly payments shown in the interface Programming with Microsoft Visual Basic 2010, 5th Edition

Lesson B Summary To nest repetition structure: To refresh interface: 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)

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

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

Figure 6-39 TOE chart for the Shopper’s Haven application Programming with Microsoft Visual Basic 2010, 5th Edition

Including a List Box in an Interface 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

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

Figure 6-41 Syntax and examples of the Items collection’s Add method Programming with Microsoft Visual Basic 2010, 5th Edition

Adding Items to a List Box (cont’d.) Figure 6-42 Result of processing the code from Figure 6-41 Programming with Microsoft Visual Basic 2010, 5th 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

Coding the Shopper’s Haven Application Enter the appropriate Add methods: In the form’s Load event procedure Follow instructions on page 378

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

The SelectedItem and SelectedIndex Properties (cont’d.) Figure 6-45 Second item selected in the list box

Figure 6-46 Examples of the list box’s SelectedItem and SelectedIndex properties Programming with Microsoft Visual Basic 2010, 5th Edition

Figure 6-47 Examples of selecting the default list box item Programming with Microsoft Visual Basic 2010, 5th Edition

Figure 6-48 Default item selected in the list box Programming with Microsoft Visual Basic 2010, 5th Edition

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

Coding the btnCalc Control’s Click Event Procedure Figure 6-49 Pseudocode for the btnCalc control’s Click event procedure

Coding the btnCalc Control’s Click Event Procedure (cont’d.) Figure 6-50 Calculated amounts shown in the interface Programming with Microsoft Visual Basic 2010, 5th 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

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