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
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.
9.1 Test-Driving the Car Payment Calculator Application
9.1 Test-Driving the Car Payment Calculator Application Figure 9.1 Car Payment Calculator application before data has been entered. ListBox control
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
9.1 Test-Driving the Car Payment Calculator Application Figure 9.3 Car Payment Calculator application displaying calculation results. Results displayed in tabular format
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; }
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.3 Constructing the Car Payment Calculator Application
9.3 Constructing the Car Payment Calculator Application
9.3 Constructing the Car Payment Calculator Application Figure 9.6 Car Payment Calculator application’s Form in design mode.
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
9.3 Constructing the Car Payment Calculator Application Figure 9.8 Rearranging and commenting the ListBox control declaration.
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
9.3 Constructing the Car Payment Calculator Application Figure 9.9 Clearing the contents of a ListBox.
9.3 Constructing the Car Payment Calculator Application Adding a header to the ListBox Call method Add Escape sequences \t
9.3 Constructing the Car Payment Calculator Application Figure 9.10 Adding a header to a ListBox.
9.3 Constructing the Car Payment Calculator Application
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
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
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
9.3 Constructing the Car Payment Calculator Application Figure 9.14 Determining amount borrowed and monthly interest rate.
9.3 Constructing the Car Payment Calculator Application Setting the loop-continuation condition Counter-controlled repetition Uses a counter variable Also called definite repetition
9.3 Constructing the Car Payment Calculator Application Figure 9.15 Loop-continuation condition.
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
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
9.3 Constructing the Car Payment Calculator Application Figure 9.17 Calculating the monthly payment
9.3 Constructing the Car Payment Calculator Application Displaying the result Use method Add Use method String.Format to display values in currency format
9.3 Constructing the Car Payment Calculator Application Figure 9.18 Displaying the number of months and the amount of each monthly payment.
9.3 Constructing the Car Payment Calculator Application Increment the counter Add one to the counter Will terminate the loop when value becomes 6
9.3 Constructing the Car Payment Calculator Application Figure 9.19 Incrementing the counter.
CarPayment-Calculator.cs (1 of 6)
CarPayment-Calculator.cs (2 of 6) Declaration for a ListBox control
CarPayment-Calculator.cs (3 of 6)
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
CarPayment-Calculator.cs (5 of 6) Using a while repetition statement Using the Math.Pow method Incrementing the counter
CarPayment-Calculator.cs (6 of 6)