tutorial 6 – car payment calculator application: introducing the while repetition statement

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

Upload: azizi

Post on 04-Jan-2016

37 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

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

© 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

Outline6.1 Test-Driving the Car Payment Calculator Application6.2 while Repetition Statement6.3 Increment and Decrement Operators6.4 Constructing the Car Payment Calculator Application6.5 do…while Repetition Statement6.6 Wrap-Up

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

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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.1 Test Driving the Car Payment Calculator Application

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.1 Test 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.

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.1 Test Driving the Car Payment Calculator Application (Cont.)

Figure 6.3  Car Payment Calculator application displaying calculation results.

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

© 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 listPurchase next itemCross 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

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

© 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

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

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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Operator Called Sample expression Explanation ++ preincrement ++counter Increment counter by 1, then use

the new value of counter in the expression in which counter resides.

++ postincrement counter++ Use the current value of counter in the expression in which counter resides, then increment counter by 1.

-- predecrement --counter Decrement counter by 1, then use the new value of counter in the expression in which counter resides.

-- postdecrement counter-- Use the current value of counter in the expression in which counter resides, then decrement counter by 1.

Figure 6.5 Increment and decrement operators.

6.3 Increment and Decrement Operators (Cont.)

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Initialize the loan length to two years Prompt the user for and input the car price, down payment and annual interest rate Calculate the loan amount Calculate the monthly interest rate While the loan length is less than or equal to five years Calculate the number of months Calculate the monthly payment based on the loan amount, monthly interest rate and loan length in months Display the result (in tabular format) Increment the loan length by one year

Figure 6.6 Pseudocode for the Car Payment Calculator application.

6.4 Constructing the Car Payment Calculator Application

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.4 Constructing 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

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

© 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.4 Constructing the Car Payment Calculator Application (Cont.)

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

© 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 <iomanip> header file

• Displays:

6.4 Constructing the Car Payment Calculator Application (Cont.)

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.4 Constructing 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 <iostream> 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.4 Constructing 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.4 Constructing 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 6.12  Displaying a table header.

6.4 Constructing the Car Payment Calculator Application (Cont.)

Table header

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.4 Constructing 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.4 Constructing 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.4 Constructing 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 6.18  Output from the completed application.

6.4 Constructing the Car Payment Calculator Application (Cont.)

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

© 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

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

© 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

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

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

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

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

6.5 do…while Repetition Statement

Start of do…while statement

Incrementing years for next iteration

Condition of do…while statement

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

© 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 // Tutorial 6: CarPayment.cpp

2 // Calculate different billing plans for a car loan.

3 #include <iostream> // required to perform C++ stream I/O

4 #include <iomanip> // required for parameterized stream manipulators

5 #include <cmath> // required to use the C++ math library functions

6

7 using namespace std; // for accessing C++ Standard Library members

8

9 // calculate monthly payment

10 double calculateMonthlyPayment( double monthlyInterestRate,

11 int months, int loanAmount )

12 {

13 double base = pow( 1 + monthlyInterestRate, months );

14

15 return loanAmount * monthlyInterestRate / ( 1 - ( 1 / base ) );

16

17 } // end function calculateMonthlyPayment

18

CarPayment.cpp

(1 of 4)

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

19 // function main begins program execution

20 int main()

21 {

22 // define variables

23 int years = 2; // length of the loan in years

24 int months; // payment period

25 double monthlyPayment; // monthly payment

26 int price; // price of the car

27 int downPayment; // down payment

28 double interestRate; // annual interest rate

29

30 // prompt user for and input car price,

31 // down payment and annual interest rate

32 cout << "Enter car price: ";

33 cin >> price; // get price

34

35 cout << "Enter down payment: ";

36 cin >> downPayment; // get down payment

37

38 cout << "Enter annual interest rate: ";

39 cin >> interestRate; // get annual interest rate

40

CarPayment.cpp

(2 of 4)

Define variables

Prompt for and input car price, down payment and annual interest rate

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

41 // add a header to the output

42 cout << left << setw( 10 ) << "Months"

43 << "Monthly Payments" << endl;

44

45 // calculate the loan amount and monthly interest rate

46 int loanAmount = price - downPayment;

47 double monthlyInterestRate = interestRate / 1200;

48

49 // set precision for output values

50 cout << fixed << setprecision( 2 );

51

CarPayment.cpp

(3 of 4)

Display a table header

Calculate the loan amount and monthly interest

Format floating-point values as currency

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

52 // process loans of 2, 3, 4, and 5 years

53 while ( years <= 5 )

54 {

55 // calculate payment period in months

56 months = 12 * years;

57

58 // calculate monthlyPayment

59 monthlyPayment = calculateMonthlyPayment(

60 monthlyInterestRate, months, loanAmount );

61

62 // insert result into output stream

63 cout << left << setw( 10 ) << months

64 << "$" << monthlyPayment << endl;

65

66 years++; // increment the counter

67

68 } // end while

69

70 cout << "\n"; // insert newline for readability

71

72 return 0; // indicate that program ended successfully

73

74 } // end function main

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