Professional Documents
Culture Documents
Lect06 Introducing The Do While... Loop and Do Until... Loop Repetition Statements
Lect06 Introducing The Do While... Loop and Do Until... Loop Repetition Statements
Lect06 Introducing The Do While... Loop and Do Until... Loop Repetition Statements
T U T O R I A L
Car Payment
9
Calculator Application
Introducing the Do While...Loop
and Do Until...Loop Repetition
Statements
Outline
Objectives
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.
ListBox control
Figure 9.1 | Car Payment Calculator application before data has been entered.
Figure 9.2 | Car Payment Calculator application after data has been entered.
calculateButton Click
Figure 9.6 | Car Payment Calculator application ACE table. Part (1 of 2.)
Figure 9.6 | Car Payment Calculator application ACE table. Part (2 of 2.)
Variables to store
calculation results
■ 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).
Figure 9.16 | Displaying the number of months and the amount of each monthly
payment.
(2 of 3 )
17
18 ' add header to ListBox
19 paymentsListBox.Items.Add("Months" & ControlChars.Tab & _
20 ControlChars.Tab & "Monthly Payments") Add a header to the ListBox
21
22 ' retrieve user input and assign values
23 ' to their respective variables
24 downPayment = Val(downPaymentTextBox.Text)
25 price = Val(priceTextBox.Text)
26 interest = Val(interestTextBox.Text) / 100
27
28 ' determine amount borrowed and monthly interest rate
29 loanAmount = price - downPayment
30 monthlyInterest = interest / 12
31
(3 of 3 )
32 ' calculate payments for two, three, four and five year loans
33 Do While years <= 5 Do While...Loop repeats its
34 ' calculate payment period body while years is less than or
35 months = 12 * years equal to 5
36
37 ' calculate monthly payment using Pmt Calculate term in months
38 monthlyPayment = _
39 Pmt(monthlyInterest, months, -loanAmount) Calculate monthly
40 payment amount
41 ' display payment value
42 paymentsListBox.Items.Add(months & ControlChars.Tab & _
43 ControlChars.Tab & String.Format("{0:C}", _
44 monthlyPayment)) Display number of months
and monthly payment
45
amount
46 years += 1 ' increment counter
47 Loop Increment counter years so
48 End Sub ' calculateButton_Click the loop-continuation
49 End Class ' CarPaymentCalculatorForm condition eventually becomes
false