Upload
rickey-tarte
View
230
Download
1
Tags:
Embed Size (px)
Citation preview
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
DOWHILE condition p is truestatement block
ENDDO
WHILE condition p is truestatement block
ENDWHILE
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
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
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
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
Programming Logic and Design, Fifth Edition, Comprehensive 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
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
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
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 somethingendfor
Using a for Loop (continued) Example:
for count = 0 to 99print 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
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
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