Download presentation
Presentation is loading. Please wait.
Published byScarlett Watts Modified over 8 years ago
1
Chapter 05 (Part II) Control Statements: Part II
2
Objectives In part II you will learn: To get familiar with the use of for statements. To use other data type and C++ libraries. To use the do…while repetition statements.
3
Calculating Amount on Deposit C++ treats floating-point values as type double
4
Declare Floating-Point Variable Use float or double to declare a floating- point variable. –float type: single precision –double type: single precision –Note: Floating-point values are approximate, so controlling counting loops with floating-point variables can result in imprecise counter values and inaccurate tests for termination.
5
Calculating Amount on Deposit
6
Formatting Numeric Output Stream manipulator std:: setw –Setting field width –Must include iomanip #include –Right justified by default Stream manipulator std:: left to left-justify Stream manipulator std:: right to right-justify –Applied only to the next output value
7
Formatting Numeric Output Example: cout << “Age: ” << left() << setw(3) << age << “ years old” << endl; cout << “Age: ” << right() << setw(3) << age << “ years old” << endl; cout << “Age: ” << setw(3) << age << “ years old” << endl;
8
Formatting Numeric Output Stream manipulators fixed and setprecision –Sticky settings Remain the next stream manipulator in effect until they are changed. Example cout << fixed << setprecision(2); –cout will not actually display anything but change the output settings. –‘fixed’ will not work on setw().
9
Calculating Amount on Deposit
10
5.4 Examples Using the for Statement Standard library function std::pow –Calculates an exponent –pow( x, y ) Calculates the value of x y The computation result is a value with double type. –Example: double x = 4, y = 2.3; double result = std::pow(x, y); –Requires header file
11
Common Programming Error Forgetting to include the appropriate header file when using standard library functions is a compilation error. Example: #include int main() { double x = 4, y = 2.3; double result = std::pow(x, y); cout << setw(10) << result; return 0; }
12
C++ treats floating-point values as type double setw stream manipulator will set a field width standard library function pow (in header file ) Specify that the next value output should appear in a field width of 21
13
Calculate amount within for statement Use the setw stream manipulator to set field width Principal*(1+rate) year
14
5.5 do … while Repetition Statement do…while statement –Similar to while statement –Testing condition after performing the body Loop body always executes at least once An semicolon must be added right after a do…while statement. –Example: do { body } while (condition) ; Executes at least once.
15
Good Programming Practice Always including braces in a do...while statement helps eliminate ambiguity between the while statement and the do...while statement containing one statement. Example: int option; do { cin >> option; } while (option != -1);
16
Declare and initialize control variable counter do…while loop displays counter ’s value before testing for counter ’s final value
17
Flowchart diagram for the do...while repetition statement
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.