T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Advertisements

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.
 Control structures  Algorithm & flowchart  If statements  While statements.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Fundamentals of Python: From First Programs Through Data Structures
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
Fundamentals of Python: First Programs
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Microwave Oven Application Building Your Own Classes and Objects.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
Repetition Statements
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
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()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 7 Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Problem Solving and Control Statements: Part 2
Introducing Do While & Do Until Loops & Repetition Statements
Tutorial 12 – Security Panel Application Introducing the Select Case Multiple-Selection Statement Outline Test-Driving the Security Panel Application.
Chapter 3: Introduction to Problem Solving and Control Statements
3 Control Statements:.
CIS 16 Application Development Programming with Visual Basic
Chapter 6 Control Statements: Part 2
Presentation transcript:

T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop Repetition Statements

 2009 Pearson Education, Inc. All rights reserved. 2 Outline 9.1 Test-Driving the Car Payment Calculator Application 9.2 Do While...Loop Repetition Statement 9.3 Do Until...Loop Repetition Statement 9.4 Constructing the Car Payment Calculator Application

 2009 Pearson Education, Inc. All rights reserved. 3 In this tutorial you will learn: ■Use the Do While...Loop and Do Until...Loop repetition statements to execute statements in an application repeatedly. ■Use counter-controlled repetition. ■Display information in ListBox es. ■Concatenate strings. Objectives

Application Requirements  2009 Pearson Education, Inc. All rights reserved Test-Driving the Car Payment Calculator Application Typically, banks offer car loans for periods ranging from two to five years. Borrowers repay the loans in monthly installments. The amount of each monthly payment is based on the length of the loan, the amount borrowed and the interest rate. Create an application that allows the customer to enter the price of a car, the down-payment amount and the annual interest rate of the loan. The application should display the loan’s duration in months and the monthly payments for two-, three-, four- and five-year loans. The variety of options allows the user to easily compare repayment plans and choose the most appropriate.

 2009 Pearson Education, Inc. All rights reserved. 5 Figure 9.1 | Car Payment Calculator application before data has been entered. Test-Driving the Car Payment Calculator Application ListBox control ■Note the new GUI control—the ListBox control, which allows users to view and/or select from multiple items in a list (Fig. 9.1).

 2009 Pearson Education, Inc. All rights reserved. 6 Figure 9.2 | Car Payment Calculator application after data has been entered. Test-Driving the Car Payment Calculator Application (Cont.) ■Enter data to test the application (Fig. 9.2).

 2009 Pearson Education, Inc. All rights reserved. 7 Figure 9.3 | Car Payment Calculator application displaying calculation results. Test-Driving the Car Payment Calculator Application (Cont.) ■Click the Calculate Button. ■The application displays the monthly payment amounts in the ListBox (Fig. 9.3) in a tabular format. Results displayed in tabular format

 2009 Pearson Education, Inc. All rights reserved. 8 ■A repetition statement can repeat actions, depending on the value of a condition. ■If you go to the grocery store with a list of items to purchase, you go through the list until you have each item: Do while there are more items on my shopping list Put next item in cart Cross it off my list 9.2 Do While... Loop Repetition Statement

 2009 Pearson Education, Inc. All rights reserved. 9 ■Using a Do While... Loop statement, this code finds the first power of 3 greater than 50. Dim product As Integer = 3 DoWhile product <= 50 product *= 3 Loop ■The condition in the Do While... Loop statement, product <= 50, is referred to as the loop-continuation condition. ■When the loop-continuation condition becomes false, the Do While... Loop statement finishes executing. 9.2 Do While... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 10 Good Programming Error Provide in the body of every Do While...Loop statement an action that eventually causes the condition to become false. If you do not, the repetition statement never terminates, causing an error called an infinite loop. Such an error causes the application to “hang up.” When an infinite loop occurs in your application, return to the IDE and select Debug > Stop Debugging.

 2009 Pearson Education, Inc. All rights reserved. 11 Figure 9.4 | Do While...Loop repetition statement UML activity diagram. ■The UML activity diagram in Fig. 9.4 illustrates the flow of control in the preceding Do While... Loop repetition statement. 9.2 Do While... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 12 ■The transition arrow emerging from the action state points back to the merge, creating a loop. ■The UML’s diamond-shaped merge symbol joins two flows of activity into one. 9.2 Do While... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 13 ■These statements describe the repetitive actions that occur during a shopping trip: Do until there are no more items on my shopping list Put next item in cart Cross it off my list ■Statements in the body of a Do Until... Loop are executed repeatedly for as long as the loop- termination condition remains False. –This is known as a loop-termination condition. 9.3 Do Until... Loop Repetition Statement

 2009 Pearson Education, Inc. All rights reserved. 14 ■Using a Do Until... Loop, this code finds the first power of 3 larger than 50 : Dim product As Integer = 3 Do Until product > 50 product *= 3 Loop 9.3 Do Until... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 15 Good Programming Error Failure to provide the body of a Do Until...Loop statement with an action that eventually causes the condition in the Do Until...Loop to become true creates an infinite loop.

 2009 Pearson Education, Inc. All rights reserved. 16 Figure 9.5 | Do Until... Loop repetition statement UML activity diagram. ■The UML activity diagram in Fig. 9.5 illustrates the flow of control for the Do Until... Loop repetition statement. 9.3 Do Until... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 17 When the user clicks the Calculate Button Initialize loan length to two years Clear the ListBox of any previous calculation results Add a header to the ListBox Get down payment from a TextBox Get sticker price from a TextBox Get annual interest rate from a TextBox Calculate loan amount (sticker price – down payment) Calculate monthly interest rate (annual interest rate / 12) 9.4 Constructing the Car Payment Calculator Application

 2009 Pearson Education, Inc. All rights reserved. 18 Do while loan length is less than or equal to five years Convert the loan length in years to number of months Calculate monthly payment based on loan amount, monthly interest rate and loan length in months Insert result into ListBox Increment loan length in years by one year 9.4 Constructing the Car Payment Calculator Application (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 19 Action/Control/Event (ACE) Table for the Car Payment Calculator Figure 9.6 | Car Payment Calculator application ACE table. Part (1 of 2.) ■Use an ACE table to convert pseudocode into Visual Basic (Fig. 9.6).

 2009 Pearson Education, Inc. All rights reserved. 20 Action/Control/Event (ACE) Table for the Car Payment Calculator (Cont.) Figure 9.6 | Car Payment Calculator application ACE table. Part (2 of 2.)

 2009 Pearson Education, Inc. All rights reserved. 21 Figure 9.7 | ListBox added to Car Payment Calculator application’s Form. Adding a ListBox to the Car Payment Calculato r Application ■Add a ListBox control from the Toolbox. ■Change the ListBox ’s Name property to paymentsListBox. Set the Location property to 24, 166 and the Size property to 230, 94 (Fig. 9.7).

 2009 Pearson Education, Inc. All rights reserved. 22 Good Programming Practice Append the ListBox suffix to all ListBox control names.

 2009 Pearson Education, Inc. All rights reserved. 23 GUI Design Tip A ListBox should be large enough to display all of its contents or large enough that scrollbars can be used easily.

 2009 Pearson Education, Inc. All rights reserved. 24 Figure 9.8 | Clearing the contents of a ListBox. Using Code to Change a ListBox ’s Contents ■Double click the Calculate Button to generate the empty event handler (Fig. 9.8). ■Content can be added and deleted from the ListBox by using its Items property. –The Items property returns an object that contains a list of items displayed in the ListBox.

 2009 Pearson Education, Inc. All rights reserved. 25 Figure 9.9 | Adding a header to a ListBox. ■The ListBox displays the number of monthly payments and the amount per payment. ■To clarify what information is being displayed, a line of text—called a header—is added to the ListBox by the Method Add (lines 10–11 of Fig. 9.9) Using Code to Change a ListBox ’s Contents (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 26 GUI Design Tip Use headers in a ListBox when you are displaying tabular data. Adding headers improves readability by describing the information that is displayed in the ListBox.

 2009 Pearson Education, Inc. All rights reserved. 27 ■The ampersand symbol ( & ) is called the string- concatenation operator. This operator combines its two operands into one string value (used to join text). ■The constant ControlChars.Tab inserts a tab character into the string. Using Code to Change a ListBox ’s Contents (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 28 Figure 9.10 | Variables for the Car Payment Calculator application. ■The calculation requires the length in months, but the loop-continuation condition uses the number of years (Fig. 9.10). Declaring Variables and Retrieving User Input Variables to store the length of the loan Variables to store user input Variables to store calculation results

 2009 Pearson Education, Inc. All rights reserved. 29 Figure 9.11 | Retrieving input in the Car Payment Calculator application. ■Note that line 26 (Fig. 9.11) divides the interest rate by 100 to obtain the decimal equivalent –For example, 5% becomes.05 Declaring Variables and Retrieving User Input (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 30 Figure 9.12 | Determining amount borrowed and monthly interest rate. ■The application computes the amount of the loan by subtracting the down payment from the price. ■These calculations need to occur only once, so they are placed before the Do While... Loop statement (Fig. 9.12). Declaring Variables and Retrieving User Input (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 31 Figure 9.13 | Loop-continuation condition. ■After you type line 33 and press Enter, the IDE will close the repetition statement by adding the keyword Loop in line 35 (Fig. 9.13). Calculating the Monthly Payment Amounts with a Do While... Loop Repetition Statement

 2009 Pearson Education, Inc. All rights reserved. 32 ■This loop is an example of counter-controlled repetition. –This uses a counter ( years ) to control the number of times that a set of statements executes. –Counter-controlled repetition also is called definite repetition, because the number of repetitions is known before the repetition statement begins. Calculating the Monthly Payment Amounts with a Do While... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 33 Figure 9.14 | Converting the loan duration from years to months. ■The number of months changes with each iteration of this loop, and the calculation result changes based on the length of the payment period (Fig. 9.14). Calculating the Monthly Payment Amounts with a Do While... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 34 ■The Pmt function returns a Double value that specifies the monthly payment amount on a loan for a constant interest rate and a given time period (Fig. 9.15). ■Borrowed amounts are represented by negative values. Calculating the Monthly Payment Amounts with a Do While... Loop Repetition Statement (Cont.) Figure 9.15 | Pmt function returns monthly payment.

 2009 Pearson Education, Inc. All rights reserved. 35 Figure 9.16 | Displaying the number of months and the amount of each monthly payment. ■The number of monthly payments and the monthly payment amounts are displayed beneath the header in the ListBox. ■ String.Format is used to display monthlyPayment in currency format (Fig. 9.16). Calculating the Monthly Payment Amounts with a Do While... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 36 Figure 9.17 | Incrementing the counter. ■The counter variable years is incremented in each iteration until it equals 6 (Fig. 9.17). ■Then the loop-continuation condition ( years <= 5 ) evaluates to False and the repetition ends. Calculating the Monthly Payment Amounts with a Do While... Loop Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 37 ■Figure 9.18 presents the source code for the application. Outline (1 of 3 ) Clear the ListBox

 2009 Pearson Education, Inc. All rights reserved. 38 Outline (2 of 3 ) Add a header to the ListBox

 2009 Pearson Education, Inc. All rights reserved. 39 Outline (3 of 3 ) Do While...Loop repeats its body while years is less than or equal to 5 Calculate term in months Calculate monthly payment amount Display number of months and monthly payment amount Increment counter years so the loop-continuation condition eventually becomes false