1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
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 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled.
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.
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.
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.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
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 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 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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Control Structures Repetition or Iteration or Looping Part II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
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.
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.
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.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
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)
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.
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?
C++ Iterative Constructs
Chapter 6 Looping.
Branching Constructs Review
Programming Fundamentals
Conditinoal Constructs Review
Additional Control Structures
Conditional Construct
Lecture Notes – Week 3 Lecture-2
Conditinoal Constructs Review
Loops A loop is a repetition control structure.
Counting Loops.
Repetition Control Structure
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Presentation transcript:

1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to Control Input Data l Using a While Statement for Summing and Counting l Nested While Loops l Loop Testing and Debugging

3 Revision of Selection l What construct is used in C++ to narrow down our range of values? l What is the rule for matching else constructs to if constructs? l What is the danger in using an if followed by another if? l For testing selection control structures, should we use white box testing or black box testing?

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

5 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

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

7 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

8 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

26 User has to give 100 blood pressure values observed for a patient Use a while loop to read the 100 blood pressure values and find their average Count-Controlled Loop Example

27 int thisBP ; int total ; int count ; count = 0 ; // initialize while ( count < 100 ) // test expression { cin >> thisBP ; total = total + thisBP ; count++ ; // update } cout << “The average = “ << ((float) total)/100.0 << endl ;