Chapter 05 (Part II) Control Statements: Part II.

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Computer Science 1620 Loops.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
4 Control Statements.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Chapter 3: Input/Output
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
CPS120: Introduction to Computer Science Formatted I/O.
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.
Chapter 3: Input/Output
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
A First Book of C++ Chapter 5 Repetition.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Control Structures Sequential execution Transfer of control
Chapter 3 L7.
Programming Fundamentals
Control Statements: Part 2
MSIS 655 Advanced Business Applications Programming
Chapter 3: Input/Output
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Let’s all Repeat Together
C++ for Engineers and Scientists Second Edition
Presentation transcript:

Chapter 05 (Part II) Control Statements: Part II

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.

Calculating Amount on Deposit C++ treats floating-point values as type double

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.

Calculating Amount on Deposit

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

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;

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

Calculating Amount on Deposit

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

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; }

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

Calculate amount within for statement Use the setw stream manipulator to set field width Principal*(1+rate) year

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.

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);

Declare and initialize control variable counter do…while loop displays counter ’s value before testing for counter ’s final value

Flowchart diagram for the do...while repetition statement