Download presentation
Presentation is loading. Please wait.
Published byHarvey Cook Modified over 9 years ago
1
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
2
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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.
3
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.1Test Driving the Car Payment Calculator Application
4
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.1Test Driving the Car Payment Calculator Application (Cont.) Figure 6.1 Car Payment Calculator application before data has been entered. Figure 6.2 Car Payment Calculator application after data has been entered.
5
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.1Test Driving the Car Payment Calculator Application (Cont.) Figure 6.3 Car Payment Calculator application displaying calculation results.
6
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.2 while Repetition Statement Find the first power of 3 greater than 50 While there are still items on my shopping list Purchase next item Cross it off my list Pseudocode Repetition statement –Repeats actions, depending on the value of a condition Loop-continuation condition –Loop executes while condition remains true
7
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
8
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 6.4 while repetition statement UML activity diagram. 6.2 while Repetition Statement (Cont.)
9
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.3Increment 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
10
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.3Increment and Decrement Operators (Cont.)
11
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application
12
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Loop continuation condition will be number of years Figure 6.7 Define variables that are used in the Car Payment Calculator. Define variables to store user input and calculated values
13
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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.4Constructing the Car Payment Calculator Application (Cont.)
14
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 header file Displays: 6.4Constructing the Car Payment Calculator Application (Cont.)
15
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Use headers to improve readability Left stream manipulator –Indicates that values should be left justified in their output field –Accessed using the header file –The left stream manipulator applies to all output until the end of execution or that format is changed Figure 6.9 Formatting output using the setw and left stream manipulators. Displaying a table header using the setw and left stream manipulators
16
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Dividing two int values when the result should be a floating- point value is a logic error Figure 6.10 Determining the amount borrowed and the monthly interest rate. Calculate the loan amount and the monthly interest rate
17
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Figure 6.11 Displaying the result in currency format. Use fixed and setprecision to format output as dollar amounts
18
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 6.12 Displaying a table header. 6.4Constructing the Car Payment Calculator Application (Cont.) Table header
19
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Counter controlled repetition (also called definite repetition) –Counter variable ( years ) controls number of times loop iterates –Number of repetitions is known before loop executes Figure 6.13 Adding the while statement. Insert the while statement
20
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator 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
21
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Figure 6.16 Displaying the number of months and the amount of each monthly payment. Display the monthly payment Counter variable ( years ) incremented until the loop continuation condition ( years <= 5 ) evaluates to false. Figure 6.17 Incrementing the counter. Increment the years counter
22
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 6.18 Output from the completed application. 6.4Constructing the Car Payment Calculator Application (Cont.)
23
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
24
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.5 do…while Repetition Statement (Cont.) do…while code example
25
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.5 do…while Repetition Statement (Cont.) Figure 6.20 do…while repetition statement UML activity diagram.
26
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 6.21 Car Payment Calculator using a do…while repetition statement. Textbook Error: Semicolon required after do-while condition.do-while 6.5 do…while Repetition Statement Start of do…while statement Incrementing years for next iteration Condition of do…while statement
27
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 6.22 Output from the completed application. 6.5 do…while Repetition Statement
28
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CarPayment.cpp (1 of 4)
29
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CarPayment.cpp (2 of 4) Define variables Prompt for and input car price, down payment and annual interest rate
30
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CarPayment.cpp (3 of 4) Display a table header Calculate the loan amount and monthly interest Format floating-point values as currency
31
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.