Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.

Similar presentations


Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing."— Presentation transcript:

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.

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.2 Car Payment Calculator application after data has been entered.

6 © 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.

7 © 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

8 © 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

9 © 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.)

10 © 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

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.3Increment and Decrement Operators (Cont.)

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application

13 © 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

14 © 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.)

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Stream manipulators –Parameterized stream manipulator setw() –Specifies the width of the field of the next value –Accessed using the header file –Nonparameterized stream manipulator left –Indicates that values should be left justified in their output field –Accessed using the header file Displays: 6.4Constructing the Car Payment Calculator Application (Cont.)

16 © 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 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

17 © 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

18 © 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

19 © 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

20 © 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

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.14 Converting the loan duration from years to months. Determine the number of months in a loan period

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Figure 6.15 The calculateMonthlyPayment function returns the monthly payment. Calculate the monthly payments

23 © 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

24 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.4Constructing the Car Payment Calculator Application (Cont.) Counter variable ( years ) incremented until the loop continuation condition ( years <= 5 ) evaluates to false. Figure 6.17 Incrementing the counter. Increment the years counter

25 © 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.)

26 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CarPayment.cpp (1 of 4)

27 © 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

28 © 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

29 © 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

30 © 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

31 © 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

32 © 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.

33 © 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. 6.5 do…while Repetition Statement Start of do…while statement Incrementing years for next iteration Condition of do…while statement

34 © 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


Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing."

Similar presentations


Ads by Google