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

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
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)
Chapter 6 Looping.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
CPS120 Introduction to Computer Science Iteration (Looping)
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
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 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
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.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 6. Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops –while –for –do.
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-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Control Structures Repetition or Iteration or Looping Part II.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 6 Looping.
Branching Constructs Review
Programming Fundamentals
For Loops October 12, 2017.
Conditinoal Constructs Review
Loops October 10, 2017.
Iteration with While You can say that again.
For & do/while Loops.
Conditinoal Constructs Review
Loops A loop is a repetition control structure.
Counting Loops.
Repetition Control Structure
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

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

Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop body changes and this causes the repeating to stop

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

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

an initialization of the loop control variable an expression to test for continuing the loop an update of the loop control variable to be executed with each iteration of the body Count-controlled loop contains

int count ; count = 4; // initialize loop variable while (count > 0) // test expression { cout << count << endl ; // repeated action count = count – 1; // update loop variable } cout << “Done” << endl ; Count-controlled Loop

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example Use a while loop to read the 100 blood pressures and find their total int thisBP ; int total ; int count ; count = 0 ; // initialize while ( count < 100 ) // test expression { cin >> thisBP ; total = total + thisBP ; count++ ; // update } cout << “The total = “ << total<<endl;