Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 1 Looping Chapter 6

3 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

4 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;

5 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

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

7 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

8 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?

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

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

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

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

13 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?

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

15 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

16 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?

17 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; }

18 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; }

19 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)

20 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?

21 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

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


Download ppt "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."

Similar presentations


Ads by Google