210 likes | 352 Vues
Looping. Chapter 6. Count Controlled Loops. Trapping for valid input. Using flags to control a while statement. Ending a loop with End Of File condition. Getting Looped in C++. while (condition). false. true. statement 1. statement 2. The while Statement.
E N D
Looping Chapter 6
Count Controlled Loops Trapping for valid input Using flags to control a while statement Ending a loop with End Of File condition Getting Looped in C++
while (condition) false true statement 1 statement 2 The while Statement • Loop <=> a control structure that causes a sequence of statements to be executed repeatedly • Syntax: • The statement can be multiple statements (compound) by using { . . . } while (condition) statement;
while (condition) false true statement 1 statement 2 Phases of Loop Execution • Loop entry => flow of control reaches first statement inside loop • Iteration => each pass thru the loop • Loop test => condition tested before each iteration • Loop exit => when termination condition occurs • in while statement, loop is NOT executed another time
While Loop Illustration Loop Iteration Loop Test Loop Exit Loop Entry
Loops Using the while statement • Count controlled => executes a specified number of times • Event controlled => terminates when something happens inside the loop body to signal that loop should be exited
Count Controlled Loop • Uses a Loop Control Variable (LCV) x = 0;while (x <= 10) { cout << “count = “ << x << endl; x = x + 1; } What gets printed?
Event Controlled Loops • Sentinel Controlled => look for a special flag or data value What must be known ahead of time?
Event Controlled Loops • End of File Controlled Do you need to know how many items are in the file? Why? Why not?
Event Controlled Loops • Flag controlled loop How man times will the loop run?
Other Uses for Loops • Count how many times something happens
7 Easy Steps for Loop Design • Decide on the termination condition • What is the initial value of the LCV • Where do you update the LCV • What is/are the statement(s) to be repeated • What initialization values must be set • What changes inside the loop (besides the LCV) • What should have happened when loop exits?
Amen Count Controlled Loops • Initialized • Incremented • Inspected The LCV must be ...
Designing the Process Within the Loop • What is the process to be repeated? • counting summing • calculating printing • Make sure initializations have taken place • totals set to zero • files opened, first element of file read • How is process updated • increment the counter • accumulate the total • read next item from file
The Loop Exit • Make sure state of program is correct at loop exit • counter has right number • all file contents have been processed • Verify these values • Adjust tasks as necessary • should counter be initialized to 0 or 1? • increment before or after printing?
x = 0;while (x <= 10) { y = 0; while (y <= 10) cout << x * y << endl; } Nested Logic • while loop calls for a statement to be repeated • What kinds of statements do w know that could go there? • That statement could also be a while loop cin cout if assignment while
Designing Nested Loops • Apply the “7 Easy Steps” to the outer loop • Apply the same steps to the inner loop • Note steps in nested loop below x = 0;while (x <= 10) { y = 0; while (y <= 10) cout << x * y << endl; }
Loop Testing Strategy • Rigorous testing would include verification of each of the 7 steps • Basically you should check ... • entry conditions • steps during iteration • program state at loop exit • Also check cases when loop is … - skipped entirely - done just once - executes normally - fails to exit (infinite loop)
Testing and Debugging Hints • Plan test data carefully • test all sections of the program • Beware of infinite loops • the condition never becomes false • Check termination conditions carefully x = 0; while (x >= 0) { cout << x; x = x + 1; } What should be changed?
Testing and Debugging Hints • Use debugger • step through source code with F8 key • use watch window
Testing and Debugging Hints • Use temporary cout statements • show you where you are • shows what values