Download presentation
Presentation is loading. Please wait.
Published byWhitney Cox Modified over 6 years ago
1
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement
Outline 9.1 Test-Driving the Car Payment Calculator Application 9.2 while Repetition Statement 9.3 Constructing the Car Payment Calculator Application 9.4 Wrap-Up
2
In this tutorial, you will learn to:
Objective In this tutorial, you will learn to: Use the while repetition statement to execute statements in a program repeatedly. Use counter-controlled repetition. Display information in ListBoxes.
3
9.1 Test-Driving the Car Payment Calculator Application
4
9.1 Test-Driving the Car Payment Calculator Application
Figure 9.1 Car Payment Calculator application before data has been entered. ListBox control
5
9.1 Test-Driving the Car Payment Calculator Application
Figure 9.2 Car Payment Calculator application after data has been entered. Input data Click Calculate Button
6
9.1 Test-Driving the Car Payment Calculator Application
Figure 9.3 Car Payment Calculator application displaying calculation results. Results displayed in tabular format
7
9.2 while 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: int intProduct = 3; while ( intProduct <= 50 ) { intProduct *= 3; }
8
9.2 while Repetition Statement
Figure 9.4 while repetition statement UML activity diagram. [ intProduct <= 50 ] > triple product value merge decision Corresponding C# statement: *= 3;
9
9.3 Constructing the Car Payment Calculator Application
10
9.3 Constructing the Car Payment Calculator Application
11
9.3 Constructing the Car Payment Calculator Application
Figure 9.6 Car Payment Calculator application’s Form in design mode.
12
9.3 Constructing the Car Payment Calculator Application
Figure 9.7 ListBox added to Car Payment Calculator application’s Form. ListBox’s control name displayed in design view ListBox control Change the Name property
13
9.3 Constructing the Car Payment Calculator Application
Figure 9.8 Rearranging and commenting the ListBox control declaration.
14
9.3 Constructing the Car Payment Calculator Application
Adding code to the event handler Clearing the ListBox Call method Clear on property Items Items property returns an object containing items in ListBox
15
9.3 Constructing the Car Payment Calculator Application
Figure 9.9 Clearing the contents of a ListBox.
16
9.3 Constructing the Car Payment Calculator Application
Adding a header to the ListBox Call method Add Escape sequences \t
17
9.3 Constructing the Car Payment Calculator Application
Figure 9.10 Adding a header to a ListBox.
18
9.3 Constructing the Car Payment Calculator Application
19
9.3 Constructing the Car Payment Calculator Application
Figure 9.12 Variables for the Car Payment Calculator application. Variables to store the length of the loan Variables to store user input Variables to store calculation results • Variable declarations
20
9.3 Constructing the Car Payment Calculator Application
Figure 9.13 Retrieving input in the Car Payment Calculator application. Retrieving input Use Int32.Parse and Double.Parse methods Divide interest rate by 100
21
9.3 Constructing the Car Payment Calculator Application
Calculating values used in the calculation Subtract down payment from price Monthly interest rate is interest divided by 12
22
9.3 Constructing the Car Payment Calculator Application
Figure 9.14 Determining amount borrowed and monthly interest rate.
23
9.3 Constructing the Car Payment Calculator Application
Setting the loop-continuation condition Counter-controlled repetition Uses a counter variable Also called definite repetition
24
9.3 Constructing the Car Payment Calculator Application
Figure 9.15 Loop-continuation condition.
25
9.3 Constructing the Car Payment Calculator Application
Figure 9.16 Converting the loan duration from years to months. Add the length of the loan in months
26
9.3 Constructing the Car Payment Calculator Application
Calculate monthly payment amount Use given formula Type Decimal stores monetary values Use the Convert.ToDecimal method
27
9.3 Constructing the Car Payment Calculator Application
Figure 9.17 Calculating the monthly payment
28
9.3 Constructing the Car Payment Calculator Application
Displaying the result Use method Add Use method String.Format to display values in currency format
29
9.3 Constructing the Car Payment Calculator Application
Figure 9.18 Displaying the number of months and the amount of each monthly payment.
30
9.3 Constructing the Car Payment Calculator Application
Increment the counter Add one to the counter Will terminate the loop when value becomes 6
31
9.3 Constructing the Car Payment Calculator Application
Figure 9.19 Incrementing the counter.
32
CarPayment-Calculator.cs (1 of 6)
33
CarPayment-Calculator.cs (2 of 6)
Declaration for a ListBox control
34
CarPayment-Calculator.cs (3 of 6)
35
CarPayment-Calculator.cs (4 of 6)
Using the Clear method of property Items of a ListBox Using the Add method of property Items of a ListBox
36
CarPayment-Calculator.cs (5 of 6)
Using a while repetition statement Using the Math.Pow method Incrementing the counter
37
CarPayment-Calculator.cs (6 of 6)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.