310 likes | 516 Views
Tutorial 6 – Car Payment Calculator Application: Introducing the while Repetition Statement. Outline 6.1 Test-Driving the Car Payment Calculator Application 6.2 while Repetition Statement 6.3 Increment and Decrement Operators
E N D
Tutorial 6 – Car Payment Calculator Application: Introducing the while Repetition Statement Outline 6.1 Test-Driving the Car Payment Calculator Application 6.2 while Repetition Statement 6.3 Increment and Decrement Operators 6.4 Constructing the Car Payment Calculator Application 6.5 do…while Repetition Statement 6.6 Wrap-Up
Objectives • In this tutorial, you will learn to: • Use the while repetition statement to execute statements repeatedly. • Use counter-controlled repetition. • Use the increment and decrement operators. • Use the setw and left stream manipulators.
6.1 Test Driving the CarPaymentCalculator Application (Cont.) Figure 6.1 CarPaymentCalculator application before data has been entered. Figure 6.2 Car Payment Calculator application after data has been entered.
6.1 Test Driving the CarPaymentCalculator Application (Cont.) Figure 6.3 Car Payment Calculator application displaying calculation results.
6.2 while Repetition Statement • Repetition statement • Repeats actions, depending on the value of a condition • Loop-continuation condition • Loop executes while condition remains true • Pseudocode While there are still items on my shopping list Purchase next item Cross it off my list • Find the first power of 3 greater than 50
6.2 while Repetition Statement (Cont.) • Infinite Loop • Occurs when the loop continuation condition never becomes false • UML diamond symbol • Used as merge symbol and decision symbol • Merge symbol has multiple incoming transition arrows • None of transition arrows associated with merge symbol have guard conditions • Decision symbol has multiple outgoing transition arrows
6.2 while Repetition Statement (Cont.) Figure 6.4 while repetition statement UML activity diagram.
6.3 Increment and Decrement Operators • Unary Increment Operator • Preincrement • Adds one to the value before the current statement executes • Postincrement • Adds one to the value after the current statement executes • Unary Decrement Operator • Predecrement • Subtracts one from the value before the current statement executes • Postdecrement • Subtracts one from the value after the current statement executes
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.7 Define variables that are used in the Car Payment Calculator. Define variables to store user input and calculated values • Loop continuation condition will be number of years
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.8 Prompt user for and input car price, down payment and annual interest rate. Prompt user for and input car price, down payment and annual interest rate
6.4 Constructing the CarPaymentCalculator Application (Cont.) • setw(int n) stream manipulator • Sets the number of characters to be used as the field width for the next insertion operation. • The field width determines the minimum number of characters to be written . • If the standard width of the representation is shorter than the field width, the representation is padded • Accessed using the <iomanip> header file • Displays:
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.9 Formatting output using the setw and left stream manipulators. Displaying a table header using the setw and left stream manipulators • Use headers to improve readability • Left stream manipulator • Indicates that values should be left justified in their output field • Accessed using the <iostream> header file • The left stream manipulator applies to all output until the end of execution or that format is changed
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.10 Determining the amount borrowed and the monthly interest rate. Calculate the loan amount and the monthly interest rate • Dividing two int values when the result should be a floating-point value is a logic error
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.11 Displaying the result in currency format. Use fixed and setprecision to format output as dollar amounts
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.12 Displaying a table header. Table header
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.13 Adding the while statement. Insert the while statement • Counter controlled repetition (also called definite repetition) • Counter variable (years) controls number of times loop iterates • Number of repetitions is known before loop executes
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.14 Converting the loan duration from years to months. Determine the number of months in a loan period Figure 6.15 The calculateMonthlyPayment function returns the monthly payment. Calculate the monthly payments
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.16 Displaying the number of months and the amount of each monthly payment. Display the monthly payment Figure 6.17 Incrementing the counter. • Counter variable (years) incremented until the loop continuation condition ( years <= 5) evaluates to false. Increment the years counter
6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.18 Output from the completed application.
6.5 do…while Repetition Statement • do…while repetition statement • Similar to while statement • Evaluates the loop continuation condition after the loop body has been executed • Infinite loops • Occur when loop continuation condition never becomes false • Can be avoided with code that eventually makes loop continuation condition false • Off-by-one error • Occurs when loop executes for one less or one more iteration than necessary
6.5 do…while Repetition Statement (Cont.) • do…while code example
6.5 do…while Repetition Statement (Cont.) Figure 6.20 do…while repetition statement UML activity diagram.
6.5 do…while Repetition Statement Figure 6.21 CarPaymentCalculator using a do…while repetition statement. Textbook Error: Semicolon required after do-while condition. Start of do…while statement Incrementing years for next iteration Condition of do…while statement
6.5 do…while Repetition Statement Figure 6.22 Output from the completed application.
CarPayment.cpp (1 of 4)
CarPayment.cpp (2 of 4) Define variables Prompt for and input car price, down payment and annual interest rate
CarPayment.cpp (3 of 4) Display a table header Calculate the loan amount and monthly interest Format floating-point values as currency
CarPayment.cpp (4 of 4) Begin a while statement Call the calculateMonthlyPayment function to get the monthly payment Display the monthly payment Increment the counter Right brace closes the while statement