1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 Lecture 15 Chapter 6 Looping Dale/Weems/Headington.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington.
Chapter 5: Loops and Files.
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.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 6 Looping.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
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.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Looping. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Loops and Files. 5.1 The Increment and Decrement Operators.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
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.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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?
Computer Programming -1-
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)
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
REPETITION CONTROL STRUCTURE
Chapter 6 Looping.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Additional Control Structures
Chapter 7 Additional Control Structures
Loops A loop is a repetition control structure.
Chapter 2.2 Control Structures (Iteration)
Alternate Version of STARTING OUT WITH C++ 4th Edition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops

2 Two Types of Loops 1. Count controlled loops Repeat a statement or block a specified number of times 2. Event-controlled loops Repeat a statement or block until a condition within the loop body changes that causes the repetition to stop

3 While Statement SYNTAX while (Expression) {.. // loop body. } Loop body can be a single statement, a null statement, or a block

4 When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the loop body WHILE LOOP FALSE TRUE body statement Expression

5 Count-controlled loops contain n An initialization of the loop control variable n An expression to test if the proper number of repetitions has been completed n An update of the loop control variable to be executed with each iteration of the body 1. Count-Controlled Loops

6 Count-controlled Loop Example int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count

7 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 4

8 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 4

9 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 count 4

10 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 count 3

11 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 count 3

12 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 count 3

13 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 count 2

14 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 count 2

15 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 2

16 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 1

17 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 1

18 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 1

19 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 0

20 Count-controlled Loop int count; count = 4; while(count > 0) FALSE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 0

21 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT Done count 0

22 2. Types of Event-Controlled Loops 1. Sentinel controlled Keep processing data until a special value that is not a possible data value is entered to indicate that processing should stop 2. End-of-file controlled Keep processing data as long as there is more data in the file 3. Flag controlled Keep processing data until the value of a flag changes in the loop body 22

23 A Sentinel-controlled Loop l Requires a “priming read” l A priming read is the reading of one set of data before the loop to initialize the variables in the expression

24 // Sentinel controlled loop total = 0; cout << “Enter a blood pressure(-1 to stop) ”; cin >> thisBP; // Priming read while(thisBP != -1)// While not sentinel { total = total + thisBP; cout << “Enter a blood pressure(-1 to stop)”; cin >> thisBP; } cout << total;

25 End-of-File Controlled Loop l Uses the fact that a file goes into the fail state when you try to read a data value beyond the end of the file to control the loop

26 total = 0; myInfile >> thisBP; // Priming read while(myInfile) // While last read successful { total = total + thisBP; myInfile >> thisBP; // Read another } cout << total; // End-of-file controlled loop

27 int count; char previous; char current; count = 0; inFile.get(previous);// Priming reads inFile.get(current); while(inFile) { if((current == ‘=‘) && (previous == ‘!’)) count++; previous = current; // Update inFile.get(current);// Read another }

28 initialize outer loop while (outer loop condition) {... initialize inner loop while(inner loop condition) { inner loop processing and update }... } Nested Loops 28

29 Designing Nested Loops l Analyze the algorithm. l Begin with outer loop l Functional Decomposition  When you get to where the inner loop appears, make it a separate module and we can come back to its design later

30 Do-While Loop When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the Do-while statement Statement Expression DO WHILE FALSE TRUE

31 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response has been input // && response == ‘y’ or ‘n’ { do { cin >> response; // Skips leading whitespace if ((response != ‘y’) && (response != ‘n’)) cout << “Please type y or n : “; } while ((response != ‘y’) && (response != ‘n’)); } Example of Do-While 31

32 Do-While Loop vs. While Loop l POST-TEST loop (exit-condition) l The looping condition is tested after executing the loop body l Loop body is always executed at least once l PRE-TEST loop (entry-condition) l The looping condition is tested before executing the loop body l Loop body may not be executed at all

33 For Loop SYNTAX for (initialization; test expression; update) { Zero or more statements to repeat }

34 Example of Repetition num int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT ?

35 Example of Repetition int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; num OUTPUT 1

36 Example of Repetition num OUTPUT 1 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; true

37 Example of Repetition num int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 1 1Potato

38 Example of Repetition num OUTPUT 2 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato

39 Example of Repetition num OUTPUT 2 true 1Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

40 Example of Repetition num int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 2 1Potato 2Potato

41 Example of Repetition num OUTPUT 3 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato 2Potato

42 Example of Repetition num OUTPUT 3 true 1Potato 2Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

43 Example of Repetition num int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 3 1Potato 2Potato 3Potato

44 Example of Repetition num OUTPUT 4 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato 2Potato 3Potato

45 Example of Repetition num OUTPUT 4 false 1Potato 2Potato 3Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

46 Example of Repetition num When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement 4 false int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

47 Example of Repetition num int counter; for (counter= 0; counter< 5; counter++) cout << “HELLO”; cout << “counter”<< counter; OUTPUT ?