Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Functions Computational assistants.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
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.
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.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Function basics – pp Computational assistants – improves clarity and enable software reuse. JPC and JWD © 2002 McGraw-Hill, Inc.
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)
Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Chapter 6 Looping Dale/Weems. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition.
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.
Chapter 6 Looping.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Iteration. Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
C++: Functions, Program Compilation, Libraries Modified from CS101 slides, which are by JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
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.
Programming Loops (continued).
C++ Iterative Constructs
ifstreams and ofstreams
Chapter 4 : Control Construct.
REPETITION CONTROL STRUCTURE
Chapter 6 Looping.
A mechanism for deciding whether an action should be taken
Branching Constructs Review
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
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Conditinoal Constructs Review
Counting Loops.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Control Structures Part 1
Let’s all Repeat Together
CHAPTER 4 File Processing.
ifstreams and ofstreams
COMS 261 Computer Science I
Programming Fundamental
Presentation transcript:

Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.

Averaging

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

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

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 1 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 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 6 value 5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 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 --

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 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 6 value 3

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 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 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 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 --

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 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 9 value 1

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 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 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 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 average 2.5

Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 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 average 2.5

Stays in stream until extracted Suppose input contains: 1 5 3 1 6 Execution Trace 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;

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

Better Way of Averaging The value of the input operation corresponds to true only if a successful extraction was made int numberProcessed = 0; double sum = 0; double value; while ( cin >> value ) { sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; 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 i for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl;

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

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

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

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

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

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

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

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

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

Execution Trace i 2 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

Execution Trace i 2 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

Execution Trace i 3 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

Execution Trace i 3 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

Execution Trace i 3 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

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