Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.

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

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.
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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
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.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
CONTROLLING PROGRAM FLOW
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
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.
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.
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.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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 Looping 5. The Increment and Decrement Operators 5.1.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
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?
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
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
Topic 4: Looping Statements
Chapter 4 : Control Construct.
Chapter 5: Control Structures II (Repetition)
A mechanism for deciding whether an action should be taken
Branching Constructs Review
Control Structures II (Repetition)
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Conditinoal Constructs Review
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Conditinoal Constructs Review
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
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.
COMS 261 Computer Science I
Presentation transcript:

Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from JPC and JWD © 2002 McGraw-Hill, Inc.

Overview Iteration using the while statement Iteration using the For construct Iteration using the DO construct

How to Determine the average age of children? Using 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 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; Suppose input contains: listSize 4

Execution Trace 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; numberProcessed Suppose input contains: listSize 0

Execution Trace 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; numberProcessed sum Suppose input contains: listSize 0 0

Execution Trace 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; numberProcessed sum Suppose input contains: listSize 0 0

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize 0 0 1

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize 1 1 1

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize 1 1 5

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum value Suppose input contains: listSize

Execution Trace 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; numberProcessed sum Suppose input contains: listSize 3 10 average 2.5 4

Execution Trace 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; numberProcessed sum average Suppose input contains: listSize

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

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 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? The value of the input operation corresponds to true only if a successful extraction was made

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 0

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

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

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

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!

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

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

#include 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; } File Processing

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

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

Summary While loop – while (expression) action For loop – for (init; expression1; expression 2) Do while loop – while (expression) DO