Repetition Statements (Loops) - 3. 2 The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.

Slides:



Advertisements
Similar presentations
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
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 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
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.
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 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Objectives You should be able to describe:
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 5: Control Structures II (Repetition)
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)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Fundamental Programming Fundamental Programming for Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
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.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
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)
Computer Programming -1-
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Unit 3 Lesson 9 Repetition Statements (Loops)
Chapter 5: Control Structures II (Repetition)
Control Structures II (Repetition)
Programming Fundamentals
Repetition Control Structure
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Repetition Statements (Loops) - 3

2 The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or group of statements as long as a control expression is true that is checked at the end of the loop. Because the control expression is tested at the end of the loop, a do while loop is executed at least one time. Code List shows and example of a do while loop.

3 Code List #include void main ( ) { double number, squared; do { cout << “Enter a number (Enter -999 to quit):”; cin >> number; squared = number * number; cout << number << “squared is “ << squared << endl; }while (number!= -999); }

4 do…while loops ◦ General form: do { }while ( );  The Boolean expression must have a value before it is executed at the end of the loop.  If the loop condition is true, control is transferred back to the top of the loop.  If the loop condition is false, the program continues with the first statement after the loop.  A do...while loop will always be executed at least once… why?

5 Syntax and Semantics of do… while Statements do { }while ( ); do {. } while ( ); statement false ? true

6  The condition can be any valid Boolean Expression  The Boolean Expression must have a value PRIOR to exiting the loop.  The body of the loop is treated as a compound statement even if it is a simple statement. { }  The loop control condition needs to eventually change to FALSE in the loop body  If the condition never becomes false, this results in an infinite loop. do…while Loops: Discussion

7 Errors with do while Loops Do NOT place a ; (semicolon) directly after the command do in a do while loop: int counter = 1; do; //Don’t do this! { cout << counter << end1; counter ++; } while(counter <= 10); This will result in a syntax error.

8 Comparing while with do while To help illustrate the difference between a while and a do while loop, compare the two flowcharts in figure Use a while loop when you need to test the control expression before the loop is executed the first time. Use a do while loop when the statements in the loop need to be executed at least once.

9 Figure Pretest vs. Post Test indefinite loops.

10 Choosing which loop to use. for loop ◦ when a loop is to be executed a predetermined number of times. while loop ◦ a loop repeated an indefinite number of times ◦ check the condition before the loop ◦ a loop that might not be executed (reading data) do...while ◦ a loop repeated an indefinite number of times ◦ check the condition at the end of the loop

11 Designing Correct Loops Initialize all variables properly ◦ Plan how many iterations, then set the counter and the limit accordingly Check the logic of the termination condition Update the loop control variable properly

12 Off-by-One Error int counter = 1; while (counter <= 10) { // Executes 10 passes counter++; } int counter = 1; while (counter < 10) { // Executes 9 passes counter++; }

13 Infinite Loop int counter = 1; while (counter <= 10) { // Executes 5 passes counter = counter + 2; } int counter = 1; while (counter != 10) { //Infinite Loop counter = counter + 2; } In general, avoid using != in loop termination conditions.

14 Error Trapping //”primed” while loop cout<<"Enter a score between ”<<low_double<<“ and “<<high_double; cin>>score; while((score high_double)) { cout<<“Invalid score, try again.”; //update the value to be tested in the Boolean Expression cout<<"Enter a score between ”<<low_double<<“ and “<<high_double; cin>>score; }

15 break and continue For this class do not use a break statement to terminate a loop. Only use break statements in a switch structure. Do not use continue in a loop either. Instead, use compound Boolean expressions to terminate loops.

16 Preferred Code List #include void main() { double num, squared; do { cout << "Enter a number (Enter 0 to quit): "; cin >> num; if (num != 0.0) { squared = num * num; cout << num << " squared is " << squared << endl; } }while (num!=0); }

17 Code List using while #include int main() { double num, squared; cout << "Enter a number (Enter 0 to quit): "; cin >> num; while (num!=0) { squared = num * num; cout << num << " squared is " << squared << endl; cout << "Enter a number (Enter 0 to quit): "; cin >> num; }

18 Nested Loops Nested loop ◦ when a loop is one of the statements within the body of another loop. for (k=1; k<=5; ++k) for (j=1; j<=3; ++j) cout<<(k+j)<<endl;  Each loop needs to have its own level of indenting.  Use comments to explain each loop  Blank lines around each loop can make it easier to read

19 Code List // #include void main() { int i,j; cout << "BEGIN\n"; for(i = 1; i <= 3; i++) { cout << " Outer loop: i = " << i << endl; for(j = 1; j <= 4; j++) cout << " Inner loop: j = " << j << endl; } cout << "END\n"; }

20 Repetition and Selection The use of an if statement within a loop to look for a certain condition in each iteration of the loop. ◦ Examples:  to generate a list of Pythagorean Triples  to perform a calculation for each employee  to find prime numbers