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

Slides:



Advertisements
Similar presentations
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 05 (Part II) Control Statements: Part II.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Chapter 7 JavaScript: Control Statements, Part 1
while Repetition Structure
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Chapter 4- part 2 Control Statements: Loops 1
MSIS 655 Advanced Business Applications Programming
Chapter 4 Control Statements: Loops 1
Structured Program
3 Control Statements:.
Chapter 6 Control Statements: Part 2
Presentation transcript:

© 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

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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.1Test Driving the Car Payment Calculator Application

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

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

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

© 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

© 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

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

© 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

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

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

© 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

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

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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

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

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

© 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

© 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

© 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

© 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

© 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

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

© 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

© 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