Title: Mgmt 362a Car Payment Calculator Application
19
- Mgmt 362a Car Payment Calculator Application
- Introducing the Do While...Loopand Do
Until...Loop Repetition Statements
2Objectives
- 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 ListBoxes.
- Concatenate strings.
39.1 Test-Driving the Car Payment Calculator
Application
- Typically, banks offer car loans for periods
ranging from two tofive years (24 to 60 months).
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. Createan
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 loans duration in months and
themonthly payments for two-, three-, four- and
five-year loans. Thevariety of options allows
the user to easily compare repayment plans and
choose the one that is most convenient.
49.1 Test-Driving the Car Payment Calculator
Application (Cont.)
- Figure 9.1 Car Payment Calculator application
before data has been entered.
59.1 Test-Driving the Car Payment Calculator
Application (Cont.)
- Figure 9.2 Car Payment Calculator application
after data has been entered.
- Input data
- Click Calculate Button
69.1 Test-Driving the Car Payment Calculator
Application (Cont.)
- Figure 9.3 Car Payment Calculator application
displaying calculation results.
79.2 DoWhile Loop Repetition Statement
- Loop-continuation condition
- While loop-continuation condition remains true,
loop statement executes its body repeatedly - When loop-continuation condition becomes false,
loop terminates - Code example
- Dim product As Integer 3Do While product lt
50 product 3Loop
8Common 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,
therepetition statement never terminates,
causingan error called an infinite loop. Such an
errorcauses the application to hang up. When
aninfinite loop occurs in an application, right
clickthe application in the task bar and select
Close.In the dialog box that pops up, click End
Now.
99.3 Do UntilLoop Repetition Statement
- Loop-continuation condition
- While loop-continuation condition remains false,
loop statement executes its body repeatedly - When loop-continuation condition becomes true,
loop terminates - Code example
- Dim product As Integer 3Do Until product gt
50 product 3Loop
10Common 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
becometrue creates an infinite loop.
119.4 Adding a ListBox to the Car
PaymentCalculator Application (Cont.)
- Figure 9.8 ListBox added to Car Payment
Calculator applications Form.
- ListBox control
- Change the Name property
129.4 Using Code to Change a ListBoxs Contents
- Adding code to the event handler
- Clearing the ListBox
- Call method Clear on property Items
- Items property returns an object containing items
in ListBox
139.4 Using Code to Change a ListBoxs Contents
(Cont.)
- Figure 9.9 Clearing the contents of a ListBox.
149.4 Using Code to Change a ListBoxs Contents
(Cont.)
- Adding a header to the ListBox
- Call method Add
- String-concatenation operator ()
- ControlChars.Tab constant
15GUI Design Tip
- Use headers in a ListBox when you aredisplaying
tabular data. Adding headers improves readability
by indicating the information that will be
displayed in the ListBox.
169.4 Using Code to Change a ListBoxs Contents
(Cont.)
- Figure 9.10 Adding a header to a ListBox.
179.4 Declaring Variables and Receiving User Input
- Figure 9.11 Variables for the Car Payment
Calculator application.
Variable declarations
189.4 Declaring Variables and Receiving User Input
(Cont.)
- Figure 9.12 Retrieving input in the Car Payment
Calculator application.
- Retrieving input
- Use Val function
- Divide interest rate by 100
199.4 Declaring Variables and Receiving User Input
(Cont.)
- Calculating values used in the calculation
- Subtract down payment from price
- Monthly interest rate is interest divided by 12
209.4 Declaring Variables and Receiving User Input
(Cont.)
- Figure 9.13 Determining amount borrowed and
monthly interest rate.
219.4 Calculating the Monthly Payment Amounts with
aDo While...Loop Repetition Statement
- Setting the loop-continuation condition
- Counter-controlled repetition
- Uses a counter variable
- Also called definite repetition
22Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Figure 9.14 Loop-continuation condition.
23Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Figure 9.15 Converting the loan duration from
years to months.
- Add the length of the loan in months
24Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Calculate monthly payment amount
- Use Pmt function
- Returns a Double value
- Type Decimal stores monetary values
- Use the Convert.ToDecimal method
25Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Figure 9.16 Pmt function returns monthly
payment.
26Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Displaying the result
- Use method Add
- Use method String.Format to display values in
currency format
27Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Figure 9.17 Displaying the number of months and
the amount of eachmonthly payment.
28Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Increment the counter
- Add one to the counter
- Will terminate the loop when value becomes 6
29Calculating the Monthly Payment Amounts with aDo
While...Loop Repetition Statement (Cont.)
- Figure 9.18 Incrementing the counter.
30Outline
(1 of 2 )
31Outline
(2 of 2 )