6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 ©1995-2001 Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
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.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 05 (Part III) Control Statements: Part II.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
Lecture 6 Repetition Richard Gesick.
Control Structures II (Repetition)
Java Programming: Guided Learning with Early Objects
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.
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.
Iteration with While You can say that again.
Repetition Control Structure
Chapter 6: Repetition Statements
Control Structures Part 1
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one complete execution of the body loop entry-the point where flow of control passes to the body loop test-the point at which the decision is made to (re)enter the body, or not loop exit-the point at which the iteration ends and control passes to the next statement after the loop termination condition-the condition that causes the iteration to stop Iterationcausing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping). Before considering specifics we define some general terms that apply to any iteration construct: When designing a loop, it is important to clearly identify each of these. Understanding the termination condition, and what guarantees it will eventually occur, are particularly vital.

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 2 while Loop The Boolean expression is examined before each pass through the body of the loop. If the Boolean expression is true, the loop body is executed; if it is false, execution proceeds to the first statement after the loop body.. The most versatile loop construct in C++ is the while statement. Syntactically: while The Boolean expression must be enclosed in parentheses, and can be either a single statement or a compound statement. Semantically: while true false

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 3 while Loop Example Example: #include using namespace std; int main() { const char ASTERISK = '*'; // 1 int numAst; // 2 int numPrinted = 0; // 3 cout << "How many asterisks do you want? "; // 4 cin >> numAst; // 5 while (numPrinted < numAst) { // 6 cout << ASTERISK; // 7 numPrinted++; // 8 } cout << endl; // 9 return 0; // 10 } This is a count-controlled loop. The loop control variable (LCV) is a variable whose value determines whether the loop body is executed.

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 4 while Loop Characteristics... while (numPrinted < numAst) { // 6 cout << ASTERISK; // 7 numPrinted++; // 8 } cout << endl; // 9... Some observations: -It is possible that the body of the loop will never be executed. -It is possible that the loop test condition will become false in the middle of an iteration. Even so, the remainder of the loop body will be executed before the loop test is performed and iteration stops. -The loop test must involve at least one variable whose value is (potentially) changed by at least one statement within the loop body. Why?

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 5 Count Controlled Loop Count Controlled Loopa loop that terminates when a counter reaches some limiting value The LCV (loop control variable) will be: -an integer variable used in the Boolean expression -initialized before the loop, -incremented or decremented within the loop body... cout << "How many asterisks do you want? "; // 4 cin >> numAst; // 5 // modified implementation while ( numAst > 0 ) { // 6 cout << ASTERISK; // 7 numAst--; // 8 } cout << endl; // 9...

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 6 Event Controlled Loop Event Controlled Loop:loop that executes until a specified situation arises to signal the end of the iteration.... const int CENTSPERDOLLAR = 100; // 1 string Description; // 2 int Dollars, // 3 Cents; // 4 int totalCents = 0; // 5 getline(In, Description, '\t'); // 6 In >> Dollars; // 7 In.ignore(1, '.'); // 8 In >> Cents; // 8 In.ignore(INT_MAX, '\n'); // 10 while ( In ) { // 11 totalCents = totalCents + + CENTSPERDOLLAR*Dollars + Cents; // 12 // repeat lines 6 through 10 }... Visual C Excedrin 2.99 Excedrin Migraine 3.99 How to Solve It The Art of War This loop is terminated by the event of an input failure.

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 7 Sentinel Controlled Loop Here we modify the previous input file to include a special marker line to signify the end of the price data in the file: const int CENTSPERDOLLAR = 100; // 1 const string SENTINEL = "xxxNoItemxxx"; // 2... getline(In, Description, '\t'); // 7 In >> Dollars; // 8 In.ignore(1, '.'); // 9 In >> Cents; // 10 In.ignore(INT_MAX, '\n'); // 11 while ( Description != SENTINEL ) { // 12 totalCents = totalCents + + CENTSPERDOLLAR*Dollars + Cents; // 13 // repeat lines 7 through 11 }... Visual C Excedrin 2.99 Excedrin Migraine 3.99 How to Solve It The Art of War xxxNoItemxxx 0.00 Deliver to: Joe Bob Hokie 666 Pritchard Hall... This loop is terminated by the event of reading the special sentinel value. Sentinel loop:loop that terminates when a specified marker (dummy data value) is read, signaling the end of the iteration.

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 8 Loop Design Considerations -What is the condition that terminates the loop? -How should the condition be initialized? -How should the condition be updated? -What guarantees this condition will eventually occur? -What is the process to be repeated? -How should the process be initialized? -How should the process be updated? -What is the state of the program on exiting the loop? -Are "boundary conditions" handled correctly? Questions that one should consider carefully when coding a loop:

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 9 for Loop For loops are used whenever the number of times a loops needs to execute is known or can be calculated beforehand. for (initial expression; test expression; update expression) The initial expression is performed just once, prior to loop execution. The initial expression may declare variables as well as specify initializations for them. The test expression is evaluated and if false then the next statement after the for loop is executed. If the test expression evaluates to true, then the of the for loop is executed, the update expression is performed, and test expression is evaluated again. For loops are generally count-controlled, but hybrid control is also fairly common, especially when input failure control is involved.

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 10 for Loop Example Example: #include using namespace std; int main() { const char ASTERISK = '*'; // 1 int numAst; // 2 int numPrinted; // 3 cout << "How many asterisks do you want? "; // 4 cin >> numAst; // 5 for (numPrinted =0; numPrinted < numAst; numPrinted++) { // 6 cout << ASTERISK; // 7 } cout << endl; // 8 return 0; // 9 } Compare this with the implementation on slide 3 using a while loop. The for loop is simply an alternative control structure (to the while ); any for loop can be expressed as a while loop, and vice versa.

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 11 do-while Loop The third loop construct in C++ is the do-while statement. Syntactically: The Boolean expression must be enclosed in parentheses, and can be either a single statement or a list of statements. A peculiarity is that the loop body must be enclosed in braces, even if it is a single statement. Semantically: do { } while true false do { } while ;

6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 12 Example: find first occurrence, if any, of a specific character in an input stream: do-while Example const char TOFIND = 'X'; char Next; int Skipped = -1; do { In.get(Next); Skipped++; } while (In && Next != TOFIND); if ( In ) { cout << TOFIND " found after " << Skipped << " characters were skipped." << endl; } else { cout << TOFIND " was not found." << endl; }