1 / 31

Tutorial 6 – Car Payment Calculator Application: Introducing the while Repetition Statement

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

azizi
Download Presentation

Tutorial 6 – Car Payment Calculator Application: Introducing the while Repetition Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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. 6.1 Test Driving the CarPaymentCalculator Application

  4. 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.

  5. 6.1 Test Driving the CarPaymentCalculator Application (Cont.) Figure 6.3 Car Payment Calculator application displaying calculation results.

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

  7. 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. 6.2 while Repetition Statement (Cont.) Figure 6.4 while repetition statement UML activity diagram.

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

  10. 6.3 Increment and Decrement Operators (Cont.)

  11. 6.4 Constructing the CarPaymentCalculator Application

  12. 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

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

  14. 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:

  15. 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

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

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

  18. 6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.12 Displaying a table header. Table header

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

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

  21. 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

  22. 6.4 Constructing the CarPaymentCalculator Application (Cont.) Figure 6.18 Output from the completed application.

  23. 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. 6.5 do…while Repetition Statement (Cont.) • do…while code example

  25. 6.5 do…while Repetition Statement (Cont.) Figure 6.20 do…while repetition statement UML activity diagram.

  26. 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

  27. 6.5 do…while Repetition Statement Figure 6.22 Output from the completed application.

  28. CarPayment.cpp (1 of 4)

  29. CarPayment.cpp (2 of 4) Define variables Prompt for and input car price, down payment and annual interest rate

  30. CarPayment.cpp (3 of 4) Display a table header Calculate the loan amount and monthly interest Format floating-point values as currency

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

More Related