COMS 261 Computer Science I

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Control Constructs Mechanisms for deciding when and how often an action should be taken.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
Chapter 5: Control Structures II (Repetition)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Control Structures RepetitionorIterationorLooping Part I.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Programming Loops (continued).
C++ Iterative Constructs
ifstreams and ofstreams
Chapter 4 : Control Construct.
REPETITION CONTROL STRUCTURE
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Branching Constructs Review
Programming Fundamentals
Chapter 2.2 Control Structures (Iteration)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
Iteration.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Conditinoal Constructs Review
Expression Review what is the result and type of these expressions?
Intro to Programming Week # 4 Control Structure Lecture # 8
Compound Assignment Operators in C++
Conditinoal Constructs Review
Miscellaneous Flow Control
Chapter 2.2 Control Structures (Iteration)
Chapter 4: Control Structures I (Selection)
Control Structures Part 1
Let’s all Repeat Together
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
ifstreams and ofstreams
COMS 261 Computer Science I
COMS 261 Computer Science I
Programming Fundamental
Presentation transcript:

COMS 261 Computer Science I Title: C++ Fundamentals Date: September 17, 2004 Lecture Number: 12

Announcements Project 1 Exam 1 Due 9/24/04 (next Friday) 9/22/04 (next Wednesday)

Review Boolean Algebra Boolean Type Relational Operators Operator Precedence Revisited Conditional Constructs

Outline Conditional Constructs Iterative Constructs

Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24 Is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || (!false) == (5 < 24))

Conditional Constructs Provide Ability to control whether a statement list is executed Two constructs If statement if if-else if-else-if Switch statement Left for reading

The Basic If Statement Syntax if (Expression) Action If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression Action true false

Example if (Value < 0) { Value = -Value; }

Sorting Two Numbers cout << "Enter two integers: "; int value1; cin >> value1 >> value2; if (value1 > value2) { int rememberValue1 = value1; value1 = value2; value2 = rememberValue1; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;

Semantics

The If-Else Statement Syntax if (Expression) Action1 else Action2 If Expression is true then execute Action1 otherwise execute Action2 if (v == 0) { cout << "v is 0"; } else { cout << "v is not 0"; } Check out the indentation! Expression Action1 Action2 true false

Finding the Max cout << "Enter two integers: "; int value1; cin >> value1 >> value2; int max; if (value1 < value2) { max = value2; } else { max = value1; } cout << "Maximum of inputs is: " << max << endl;

Finding the Max

Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice if-else-if statement if-else statements “glued” together Switch statement An advanced construct

An If-Else-If Statement if (nbr < 0){ cout << nbr << " is negative" << endl; } else if (nbr > 0) { cout << nbr << " is positive" << endl; } else { cout << nbr << " is zero" << endl; }

A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

Example cout << "Enter simple expression: "; int left; int right; char operator; cin >> left >> operator >> right; cout << left << " " << operator << " " << right << " = "; switch (Operator) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }

Determining Average Magnitude Calculate the average brightness of a list of five star magnitude values Can we do it? Yes, it would be easy Calculate the average brightness of a list of 8,479 stars visible from earth Can we do it Yes, but it would be tedious without the use of iteration

C++ Iterative Constructs Three constructs while statement for statement do-while statement

While Syntax

While Semantics

Computing an Average int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;

Execution Trace Suppose input contains: 1 5 3 1 6 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value --

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value --

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 3

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed; cout << "Average: " << average << endl; sum 10 value 1 average 2.5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed; cout << "Average: " << average << endl; sum 10 value 1 average 2.5

Stays in stream until extracted Execution Trace Suppose input contains: 1 5 3 1 6 Stays in stream until extracted int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed; cout << "Average: " << average << endl;

Even Better Way of Averaging int numberProcessed = 0; double sum = 0; double value; while (cin >> value) { sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; The value of the extraction operation corresponds to true only if a successful extraction was made What if list is empty?

Even Better Way of Averaging int numberProcessed = 0; double sum = 0; double value; while (cin >> value) { sum += value; ++numberProcessed; } if (numberProcessed > 0) { double average = sum / numberProcessed ; cout << "Average: " << average << endl; } else { cout << "No list to average" << endl;

The For Statement Syntax for (ForInit ; ForExpression; PostExpression) Action Example for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; }

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i 1

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i 1

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 1

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 1

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 2

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 2

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 2

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 2

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 3

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 3

Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 all done i 3

Table Revisiting const int TableSize = 20; long Entry = 1; cout << "i" << "\t\t" << "2**i" << endl; for (int i = 0; i <= TableSize; ++i) { cout << i << "\t\t" << Entry << endl; Entry *= 2; }

Table Revisiting const int TableSize = 20; long Entry = 1; cout << "i" << "\t\t" << "2**i" << endl; for (int i = 0; i < TableSize; ++i) { cout << i << "\t\t" << Entry << endl; Entry = 2 * Entry; } cout << "i is" << i << endl; // illegal The scope of i is limited to the loop!

Displaying a Diagonal SimpleWindow W("One diagonal", 5.5, 2.25); W.Open(); for (int j = 1; j <= 3; ++j) { float x = j * 0.75 + 0.25; float y = j * 0.75 - 0.25; float Side = 0.4; RectangleShape S(W, x, y, Blue, Side, Side); S.Draw(); }

Sample Display

Displaying Three Diagonals SimpleWindow W("Three diagonals", 6.5, 2.25); W.Open(); for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= 3; ++j) { float x = i - 1 + j * 0.75 + 0.25; float y = j * 0.75 - 0.25; float Side = 0.4; RectangleShape S(W, x, y, Blue, Side, Side); S.Draw(); } The scope of i includes the inner loop. The scope of j is just the inner loop.

Sample Display

int Counter1 = 0; int Counter2 = 0; int Counter3 = 0; int Counter4 = 0; int Counter5 = 0; ++Counter1; for (int i = 1; i <= 10; ++i) { ++Counter2; for (int j = 1; j <= 20; ++j) { ++Counter3; } ++Counter4; } ++Counter5; cout << Counter1 << " " << Counter2 << " " << Counter3 << " " << Counter4 << " " << Counter5 << endl;

For Into While Observation The for statement is equivalent to { ForInit; while (ForExpression) { Action; PostExpression; }

Only extracts nonblank characters Counting Characters int NumberOfNonBlanks = 0; int NumberOfUpperCase = 0; char c; while (cin >> c) { ++NumberOfNonBlanks; if ((c >= 'A') && (c <= 'Z')) { ++NumberOfUpperCase; } cout << "Nonblank characters: " << NumberOfNonBlanks << endl << "Uppercase characters: " << NumberOfUpperCase << endl; Only extracts nonblank characters

Counting All Characters char c; int NumberOfCharacters = 0; int NumberOfLines = 0; while ( cin.get(c) ) { ++NumberOfCharacters; if (c == '\n') { ++NumberOfLines } } cout << "Characters: " << NumberOfCharacters << endl << "Lines: " << NumberOfLines << endl; Extracts all characters

File Processing #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin("mydata.txt"); int ValuesProcessed = 0; float ValueSum = 0; float Value; while ( fin >> Value ) { ValueSum += Value; ++ValuesProcessed; } if (ValuesProcessed > 0) { ofstream fout("average.txt"); float Average = ValueSum / ValuesProcessed; fout << "Average: " << Average << endl; return 0; else { cerr << "No list to average" << endl; return 1;

Iteration Do’s Key Points Make sure there is a statement that will eventually terminate the iteration criterion The loop must stop! Make sure that initialization of loop counters or iterators is properly performed Have a clear purpose for the loop Document the purpose of the loop Document how the body of the loop advances the purpose of the loop

The Do-While Statement Syntax do Action while (Expression) Semantics Execute Action If Expression is true then execute Action again Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Action true Expression false

Waiting for a Proper Reply char Reply; do { cout << "Decision (y, n): "; if (cin >> Reply) Reply = tolower(Reply); else Reply = 'n'; } while ((Reply != 'y') && (Reply != 'n'));