SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Iteration in Programming: Loops and Their Applications

vivi avatar
12 diapositives · 282 Views · Sep 17, 2014

This guide explores the concept of loops, a fundamental repetition control structure in programming. It explains when to use loops, providing examples such as displaying numbers and their squares, summing integers until a sentinel value is entered, and calculating a user's age based on birth year input. The differences between count-controlled and event-controlled loops are detailed, as well as the mechanisms of nested loops and debugging techniques. With practical insights and testing tips, readers will enhance their problem-solving skills using iteration.

Understanding Iteration in Programming: Loops and Their Applications - slide 1
01 / 12Understanding Iteration in Programming: Loops and Their Applications
1 / 17

Télécharger la présentation

Understanding Iteration in Programming: Loops and Their Applications
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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)