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.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington.
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 6 Looping.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
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 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.
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.
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.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
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.
Control Structures RepetitionorIterationorLooping Part I.
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.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
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.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
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?
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 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 6 Looping.
Topics Introduction to Repetition Structures
Programming Logic and Design Fourth Edition, Comprehensive
Loop Structures.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
Control Structures - Repetition
Iteration with While You can say that again.
Loops A loop is a repetition control structure.
Chapter 6: Repetition Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
Topics Introduction to Repetition Structures
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

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 Count Controlled Loops

3 The while Statement n Loop a control structure that causes a sequence of statements to be executed repeatedly n Syntax: n The statement can be multiple statements (compound) by using {... } while (condition) statement 1 statement 2 true false while (condition) statement;

4 Phases of Loop Execution n Loop entry => flow of control reaches first statement inside loop n Iteration => each pass thru the loop n Loop test => condition tested before each iteration n Loop exit => when termination condition occurs in while statement, loop is NOT executed another timein while statement, loop is NOT executed another time while (condition) statement 1 statement 2 true false

5 While Loop Illustration Loop Entry Loop Test Loop Iteration Loop Exit

6 Loops Using the while statement n Count controlled => executes a specified number of times n Event controlled => terminates when something happens inside the loop body to signal that loop should be exited

7 Count Controlled Loop n Uses a Loop Control Variable (LCV) x = 0; while (x <= 10) { cou t << “count = “ << x << endl; x = x + 1; } What gets printed?

8 Event Controlled Loops n Sentinel Controlled => look for a special flag or data value What must be known ahead of time?

9 Event Controlled Loops n End of File Controlled Do you need to know how many items are in the file? Why? Why not?

10 Event Controlled Loops n Flag controlled loop How man times will the loop run?

11 Other Uses for Loops n Count how many times something happens

12 7 Easy Steps for Loop Design n Decide on the termination condition n What is the initial value of the LCV n Where do you update the LCV n What is/are the statement(s) to be repeated n What initialization values must be set n What changes inside the loop (besides the LCV) n What should have happened when loop exits?

13 Count Controlled Loops nInInInInitialized nInInInIncremented nInInInInspected The LCV must be... Amen

14 Designing the Process Within the Loop n What is the process to be repeated? counting summingcounting summing calculating printingcalculating printing n Make sure initializations have taken place totals set to zerototals set to zero files opened, first element of file readfiles opened, first element of file read n How is process updated increment the counterincrement the counter accumulate the totalaccumulate the total read next item from fileread next item from file

15 The Loop Exit n Make sure state of program is correct at loop exit counter has right numbercounter has right number all file contents have been processedall file contents have been processed n Verify these values n Adjust tasks as necessary should counter be initialized to 0 or 1?should counter be initialized to 0 or 1? increment before or after printing?increment before or after printing?

16 Nested Logic n while loop calls for a statement to be repeated n What kinds of statements do w know that could go there? n That statement could also be a while loop cin cout if assignment while x = 0; while (x <= 10) { y = 0; while (y <= 10) cout << x * y << endl; }

17 Designing Nested Loops n Apply the “7 Easy Steps” to the outer loop n Apply the same steps to the inner loop n Note steps in nested loop below x = 0; while (x <= 10) { y = 0; while (y <= 10) cout << x * y << endl; }

18 Loop Testing Strategy n Rigorous testing would include verification of each of the 7 steps n Basically you should check... entry conditionsentry conditions steps during iterationsteps during iteration program state at loop exitprogram state at loop exit n Also check cases when loop is … - skipped entirely - done just once - executes normally - fails to exit (infinite loop) - skipped entirely - done just once - executes normally - fails to exit (infinite loop)

19 Testing and Debugging Hints n Plan test data carefully test all sections of the programtest all sections of the program n Beware of infinite loops the condition never becomes falsethe condition never becomes false n Check termination conditions carefully x = 0; while (x >= 0) { cout << x; x = x + 1; } What should be changed?

20 Testing and Debugging Hints n Use debugger step through source code with F8 keystep through source code with F8 key use watch windowuse watch window

21 Testing and Debugging Hints n Use temporary cout statements show you where you areshow you where you are shows what valuesshows what values