Transcript Iterations
Repeating
Actions
While and For Loops
Forms of the Repetition
Structure
• For Next loops
–Count controlled
• Do While loops
–Condition controlled
2
For Next Loops
• Tells the computer to repeat one or more
statements a specified number of times.
Called a pretest loop because the loop is
evaluated before the instructions are
processed.
• For loops are used to repeat actions a
predetermined number of times.
3
For Next Loop Tasks
• The loop initializes the counter (often called the
index) variable to the startvalue (done only once,
at the beginning of the loop).
• If the stepvalue is positive, the loop tests if the
value in counter is greater than the endvalue
(when negative the test is less than). If it is, the
loop stops; otherwise the instructions within the
loop are processed.
• The loop increments the counter by the
stepvalue. It then tests the condition again.
4
For Loop in VL
The for loop
element
displays the
loop variable,
initial value,
final value, and
step by value if
it is something
other than 1
5
Nested Loops
• For loops allow code inside the body of the
loop to be repeated many times.
• A nested loop refers to a loop contained
inside the body of another loop.
6
Nested For Loops
For each time through the outer loop, the inner loop
executes from initial to final by step times.
7
Nesting At The Same Level
8
Diamond Problem
9
Do While Loops
• Do While loops are used when an action is to be
repeated an unknown number of times.
• A pre-test loop tests the looping condition before
executing the body of the loop. The body of the
loop may not be executed.
• A post-test loop guarantees at least one execution
of the loop body regardless of the condition. The
test is after the body of the loop.
10
Pre-test loop
Do It?
True
False
Note: This loop may or
may not be executed at all
11
Post-test Loop
Again?
True
False
Note: This loop will be executed
at least once
12
Sentinel Value
• A sentinel value (or signaling value) is
used to indicate the end of input.
• The solution flowchart in Figure 3-13
shows -1 as the sentinel value which is
checked twice (if and while).
13
Grocery Checkout
14
Counters and Accumulators
• Used within a repetition structure to
calculate subtotals, totals, and averages
– Initialized (usually to 0 or 1) outside the
loop and updated within the loop
– A counter is a numeric variable used for
counting something and is updated by 1
– An accumulator is a numeric variable used
for accumulating (adding together) and is
updated by an amount that varies
15
Counters and Accumulators
• Counters are variables that keep track of
how many times a statement has executed.
Counter = Counter + 1
• Accumulators are variables that maintain a
running total.
Accumulator = Accumulator + NewValue
16
Nested While Loops
17
Endless Loops
• When writing loops you must be sure that
the condition will terminate the loop or you
have what is called an endless or infinite
loop.
• When you write a loop you must be careful
to increment the variable or input a new
value inside the loop so it will terminate.
18