1 / 12

Basic Problem Solving with Programming – Iteration (or loops)

Basic Problem Solving with Programming – Iteration (or loops). What is a loop?. A loop is a repetition control structure It causes a single statement or block to be executed repeatedly while an expression is true. When use a Loop?.

vivi
Download Presentation

Basic Problem Solving with Programming – Iteration (or loops)

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. Basic Problem Solving with Programming – Iteration (or loops)

  2. What is a loop? • A loop is a repetition control structure • It causes a single statement or block to be executed repeatedly while an expression is true

  3. When use a Loop? Stated simply, one should use a loop at any point where you wish to repeat a process, idea, or function. Example: Write a small program that will display the numbers 1 – 100 and their squares.

  4. Loop Usage A. Solve the equation 2x2 + x + 5 for all integer x between 5 and 10 B. Sum inputted integers until the user enters -1 C. The user enters in the current year and then his/her birth year. Your program computes the users age. Perform this task again if he or she wishes.

  5. Two types of loops count-controlled loops repeat a specified number of times event-controlled loops something happens inside the loop body that causes the repetition to stop after the current iteration completes

  6. Count-controlled • The problem describes how many times the iteration is to executeOR • The problem indicates a starting value, ending value and increment for the loop control value. • Sometime the loop control value is needed for some calculation within the block of the loop, sometimes not.

  7. Event-controlled • Sentinel controlled - Keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop • End-of-file controlled - Keep processing data as long as there is more data in the file • Flag controlled - Keep processing data until the value of a flag changes in the loop body

  8. Sentinel-controlled loop • Requires a “priming read” • “Priming read” means you read one data value (or set of data values) before entering the while loop • Process data value(s) and then read next value(s) at end of loop

  9. Flag-controlled loops • Use meaningful name for the flag • Initialize flag (to true or false) • Test the flag in the loop test expression • Change the value of the flag in loop body when the appropriate condition occurs

  10. Nested loops • Some problems require iteration structures to be nested • Examples: • Process data in rows and columns of a table, outer loop is for one row at a time, inner loop is for each column in the current row • Outer loop is to allow the user to “play again” or “re-enter data”, inner loop is to do some iterative processing for each outer

  11. Loop Testing & Debugging • Test data should test all sections of the program • Beware of infinite loops - the program doesn’t stop • Check loop termination condition, and watch for an OBOB (off-by-1 bug) • Use algorithm walk-through to verify that appropriate conditions occur in the right places • Trace execution of loop by hand with code walk-through • Use a debugger to run program in “slow motion” or use debug output statements

  12. Scratch iteration control structures • forever • forever if (condition) • repeat until (condition) • repeat (number of times)

More Related