1 / 31

Chapter 6

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

jaeger
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 LOOPING

  2. Loop • A control structure • that causes • a sequence of statements • to be executed repeatedly

  3. While Statement • A WHILE statement • allows a loop to be executed • as long as a boolean expression is True while (logical-expression) executable-statement;

  4. While Statement Example while (inputVal != 25) cin >> inputVal; while (logical-expression) { executable-statement1; executable-statementn; }

  5. while vs. if • while (log-expr) if (log-expr) { { ... ... } } • // body executes body executes • //0 or more times 0 or 1 time

  6. 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

  7. 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

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

  9. 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.

  10. Count-controlled loops loopCount = 1; // Initialize while (loopCount <= 10) // Test { . . . loopCount = loopCount + 1; // Increment } // See CntLoop.cpp, Sumation.cpp

  11. Iteration Counter • Iteration Counter • (Loop Control Variable) • A counter variable • that is incremented with each iteration of a loop

  12. 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 }

  13. Event Controlled Loops

  14. Sentinel controlled loop • An event controlled loop which • looks for a special data value (sentinel) • as a signal the loop should be exited

  15. Sentinel is 999 while (empNum != 999) { cin >> empNum; ... } {See SentLoop.cpp}

  16. 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}

  17. BETTER cin >> empnum; while (empnum != 999) { cin >> empnum; ... // Process Data }

  18. BEST cin >> empnum; while (empnum != 999) { ... // Process Data cin >> empnum; }

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

  20. 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}

  21. 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

  22. EOLN Loop Example cin >> ch; while (ch != '\n') { cout << ch; cin >> ch; } See BlnkStrp.cpp

  23. 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

  24. cin >> empNum; moreData = EmpNum != 0; while (moreData) { . . . {moreData is Flag} . . . {Process data here} cin >> empNum; moreData = EmpNum != 0; }

  25. 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}

  26. 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?

  27. oddEmp = 0; cin >> empNum; moreData = empNum != 0; while (moreData) { if (empNum%2 == 1) oddEmp++; cin >> empNum; moreData = EmpNum != 0; }

  28. Nested Loop Pattern // Initialize outer loop while (Outer loop condition) { : // Initialize inner loop while (Inner loop condition) { // Inner loop processing & update } : // Outer loop update }

  29. cin >> starCount; while (cin) { loopCount = 1; while (loopCount <= starCount) { cout << "*"; loopCount++; } cout << endl; cin >> starCount; } cout << "Goodbye" << endl;

  30. 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

More Related