310 likes | 373 Views
Chapter 6. LOOPING. Loop. A control structure that causes a sequence of statements to be executed repeatedly. While Statement. A WHILE statement allows a loop to be executed as long as a boolean expression is True while (logical-expression) executable-statement;.
E N D
Chapter 6 LOOPING
Loop • A control structure • that causes • a sequence of statements • to be executed repeatedly
While Statement • A WHILE statement • allows a loop to be executed • as long as a boolean expression is True while (logical-expression) executable-statement;
While Statement Example while (inputVal != 25) cin >> inputVal; while (logical-expression) { executable-statement1; executable-statementn; }
while vs. if • while (log-expr) if (log-expr) { { ... ... } } • // body executes body executes • //0 or more times 0 or 1 time
Loop Entry, Iteration • Loop Entry • Point at which control first passes to the loop body • Iteration • An individual pass through, or repetition of, the body of the loop
Loop Test • The point at which the • while expression is evaluated and • the decision is made to begin a new iteration or • skip to the statement immediately following the loop
Loop exit, Termination condition • Loop exit • The point when • repetition of the loop body ends and • control passes to the first statement following loop • Termination condition • The condition that causes a loop to be exited
Contrast count-controlled and event-controlled loops. • Count-controlled loop • A loop that executes a specified number of times • Event-controlled loop • A loop that terminates • when something happens inside the loop body • to signal the loop should be exited.
Count-controlled loops loopCount = 1; // Initialize while (loopCount <= 10) // Test { . . . loopCount = loopCount + 1; // Increment } // See CntLoop.cpp, Sumation.cpp
Iteration Counter • Iteration Counter • (Loop Control Variable) • A counter variable • that is incremented with each iteration of a loop
Infinite loop • A loop whose • termination condition is never reached and • never ends without intervention from outside the program loopCount = 1; // Initialize while (loopCount <= 10) // Test { . . . loopCount = loopCount - 1; // Increment }
Sentinel controlled loop • An event controlled loop which • looks for a special data value (sentinel) • as a signal the loop should be exited
Sentinel is 999 while (empNum != 999) { cin >> empNum; ... } {See SentLoop.cpp}
Priming Read • An initial reading of a set of data values before entry into an event-controlled loop. • A priming read establishes values for variables before they are compared. {See MsNoPrim.cpp}
BETTER cin >> empnum; while (empnum != 999) { cin >> empnum; ... // Process Data }
BEST cin >> empnum; while (empnum != 999) { ... // Process Data cin >> empnum; }
EOF & EOF-controlled loops • EOF & Fail State • after last piece of data in file has been read • the computer is at end of file (EOF) • the stream state is OK • attempting to read one more data value • causes stream to enter a fail state • EOF-controlled loops • loops which terminate when the input stream • enters a fail state because of reading past EOF
EOF Loop Example cout << "Enter an integer (or Ctrl/D to quit): "; cin >> someInt; while (cin) { cout << someInt << " doubled is " << 2 * someInt << endl; cout << "Enter an integer (or Ctrl/Z to quit): "; cin >> someInt; } {See EOFPrint.cpp}
EOLN & EOLN-controlled loops • End-of-Line • refers to a position in the input stream • where a newline '\n' is placed • ('\n' is actually ASCII codes 13 & 10 WIN) • Loops which • terminate when the newline character '\n' • has been read
EOLN Loop Example cin >> ch; while (ch != '\n') { cout << ch; cin >> ch; } See BlnkStrp.cpp
What is a flag? • Flag • A boolean variable • set in one part of the program and • tested in another • to control the logical flow of the program
cin >> empNum; moreData = EmpNum != 0; while (moreData) { . . . {moreData is Flag} . . . {Process data here} cin >> empNum; moreData = EmpNum != 0; }
List three tasks commonly performed in loops. 1.) Counting each iteration 2.) Summing a set of data values 3.) Keeping track of a previous value {CountChr.cpp, SumLoop.cpp, InputCt.cpp}
7 Loop Design Points • What is the condition that ends the loop? • How should the condition be initialized? • How should the condition be updated? • What is the process being repeated? • How should the process be initialized? • How should the process be updated? • What is the state of the program on exiting the loop?
oddEmp = 0; cin >> empNum; moreData = empNum != 0; while (moreData) { if (empNum%2 == 1) oddEmp++; cin >> empNum; moreData = EmpNum != 0; }
Nested Loop Pattern // Initialize outer loop while (Outer loop condition) { : // Initialize inner loop while (Inner loop condition) { // Inner loop processing & update } : // Outer loop update }
cin >> starCount; while (cin) { loopCount = 1; while (loopCount <= starCount) { cout << "*"; loopCount++; } cout << endl; cin >> starCount; } cout << "Goodbye" << endl;
Loop Invariant • An assertion about the characteristics of a loop • These assertions must always be true for the loop to execute properly • The invariant is true on • loop entry • at the start of each loop iteration • on exit from the loop • It may not be true at each point in loop body