1 / 19

CHAPTER 5: Repetition Control Structure

CHAPTER 5: Repetition Control Structure. Objectives. To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures Introduce a pseudocode for counted repetition Develop algorithms using variations of repeat structure Learn to avoid common loop mistakes. Repeat Loop.

colm
Download Presentation

CHAPTER 5: Repetition Control Structure

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 5:Repetition Control Structure

  2. Objectives To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures Introduce a pseudocode for counted repetition Develop algorithms using variations of repeat structure Learn to avoid common loop mistakes

  3. Repeat Loop DOWHILE condition p is true statement block ENDDO WHILE condition p is true statement block ENDWHILE

  4. Advantages Looping makes computer programming efficient and worthwhile Write one set of instructions to operate on multiple, separate sets of data Loop: structure that repeats actions while some condition continues

  5. Controlling Loops As long as a Boolean expression remains true, while loop’s body executes Must control number of repetitions to avoid an infinite loop Repetitions controlled by Counter Sentinel value

  6. Controlling Loops with Counters and Sentinel Values As long as a Boolean expression remains true, DOWHILE loop’s body executes Must control number of repetitions to avoid an infinite loop Repetitions controlled by Counter Sentinel value

  7. Using a Definite while Loop with a Counter Three actions make a DOWHILE loop end correctly: Loop control variable is initialized Prior to entering the loop Loop control variable is tested If result is true, loop body entered Loop control variable must be altered in loop body DOWHILE expression eventually evaluates to false Loop control variables altered by: Incrementing Decrementing

  8. Using a Definite DOWHILE Loop with a Counter (continued)

  9. Using a Definite while Loop with a Counter (continued) Definite loop: number of iterations predetermined Also called counted loop Counter: numeric variable used to count number of times an event occurs Loop control variable may be altered by user input Indefinite loop: loop iterates until some condition is true Number of iterations may vary Programming Logic and Design, Fifth Edition, Comprehensive 9

  10. Using an Indefinite DOWHILE Loop with a Sentinel Value Indefinite loop: loop performed a different number of times each time the program executes Three crucial steps: Starting value to control the loop must be provided Comparison must be made using the value that controls the loop Within the loop, value that controls the loop must be altered Loop control variable: any variable that determines whether the loop will continue

  11. Using an Indefinite DOWHILE Loop with a Sentinel Value (continued)

  12. Using an Indefinite DOWHILE Loop with a Sentinel Value (continued)

  13. Avoiding Common Loop Mistakes Neglecting to initialize the loop control variable Neglecting to alter the loop control variable Using the wrong comparison with the loop control variable Including statements inside the loop that belong outside the loop

  14. Using a for Loop for statement or for loop is a definite loop Provides three actions in one structure Initializes Evaluates Increments Takes the form: for initialValue to finalValue do something endfor

  15. Using a for Loop (continued) Example: for count = 0 to 99 print LABEL_TEXT, name endfor Initializes count to 0 Checks count against the limit value 99 If evaluation is true, for statement body prints the label Increases count by 1

  16. Summary When using a loop, write one set of instructions that operates on multiple, separate data Three steps must occur in every loop: Initialize loop control variable Compare variable to some value Alter the variable that controls the loop

  17. Summary (continued) Common mistakes made by programmers: Neglecting to initialize loop control variable Neglecting to alter loop control variable Using wrong comparison with loop control variable Including statements inside the loop that belong outside the loop Most computer languages support a for statement for loop used with definite loops When number of iterations is known

More Related