Iteration Conditional Loops Counted Loops
Feb 25, 2016
Iteration
Conditional Loops Counted Loops
Charting the Flow of Control
• We’ve used flow charts to visualise the flow of control.
• The simplest form is sequence.
Declare vars for input and result
get input
produce result from input
show result
Charting the Flow of Control
• We’ve also used flow charts to map several different styles of selection.
• eg.If/Then/Else
Condition
TrueFalse
ProcessProcess
Flow of Control
• The third way control can be transferred is called iteration, repetition, or looping.
• There are several ways to describe a looping process. For example:– Repeat the following steps if conditions are right.
• conditional loop
– Repeat the following steps X times.• counted loop
Repeat the following steps if conditions are right.
• “if conditions are right” can mean different things:– … until there’s no more data.– … while there’s still data.
• The difference is simply in how the condition is stated.
• Using “the following steps” implies that the condition will be tested before the steps are executed. – This is called a pre-test.
Pre-test Do UntilDo Until counter = max
step1step2step3counter = counter + 1
Loop process data step1 step2…
stop?N
Y
increment counter
Pre-test Do WhileDo While counter < max
step1step2step3counter = counter + 1
Loop process data step1 step2…
repeat?Y
N
increment counter
Post-test Loops
• There are other ways of expressing conditional loops:– Repeat steps 1, 2, 3, & 4 until there’s no more data.– Repeat steps 1, 2, 3, & 4 while there’s still data.
• These are the same types of conditions.• The difference is that the condition will be tested
after the steps are executed each time. – This is called a post-test.
Post-test Do UntilDo
step1step2step3counter = counter + 1
Loop Until counter = max
process datastep1step2…
stop?N
Y
increment counter
Post-test Do WhileDo
step1step2step3counter = counter + 1
Loop While counter < max
process datastep1step2…
repeat?Y
N
increment counter
Repeat the following steps X times.
Counted loops are so common that most programming languages have a special syntax which optimises counted loops.
In VB6 this structure is implemented as the For/Next loop.
For loopIndex = start To finish‘ body of loop
Next loopIndex
For/Next LoopsDim counter As Integer
For counter = 1 To 5‘ loop body
Next counter
For/Next loops use a pre-test, and have a Do Until style.
loop body
limit exceeded?
N
Y
increment loop index
initialise loop index variable
For/Next LoopsIn the most general case, For/Next loops use 4 integer parameters.
Dim counter As Integer ‘loop indexDim start As Integer ‘initial valueDim finish As Integer ‘final valueDim increment As Integer ‘delta value
For counter = start To finish Step increment‘ loop body
Next counter
For/Next LoopsSince they are integers they can have positive or negative values._________________________________________________________
For counter = -11 To 2 Step 4‘ loop body
Next counter_________________________________________________________
What values counter will have?_________________________________________________________
For/Next LoopsSince they are integers they can have positive or negative values._________________________________________________________
For counter = -11 To 2 Step 4‘ loop body
Next counter_________________________________________________________
What values counter will have?_________________________________________________________
counter will have the values -11, -7, -3, 1.
For/Next LoopsAnother example._________________________________________________________
For counter = 10 To 0 Step -3‘ loop body
Next counter_________________________________________________________
What values counter will have?_________________________________________________________
For/Next LoopsAnother example._________________________________________________________
For counter = 10 To 0 Step -3‘ loop body
Next counter_________________________________________________________
What values counter will have?_________________________________________________________
counter will have the values 10, 7, 4, 1.
For/Next Loops
The start, finish, and increment values can be specified as literals or as Integer variables declared before the loop.
The loop index must be an Integer variable. It can be declared before the loop – or not! The For statement can act as its declaration.
For/Next LoopsThe scope of counter extends beyond the loop so its value can be used
or changed when the loop is finished. This may be a useful side effect.
Dim counter As Integer
For counter = 1 To 5‘loop body
Next counter
txtDisplay.Text = “Counter is ” & Str(Counter)
For/Next LoopsDim counter As IntegerDim start As IntegerDim finish As IntegerDim increment As IntegerFor counter = 1 To 5
start = -2 finish = 10 increment = 5
Next counter
VB ignores attempts to change the loop parameters within the loop, except…
For/Next Loops…the loop index.
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
Simulate this program to seewhat values will be displayed.
skip
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 1
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 14
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 145
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 145 5
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 145 58
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 145 589
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 145 589 9
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 145 589 9
12
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
counter display1 145 589 9
1213
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10‘display Countercounter = counter + 3
Next counter
In general this is poor programming.
counter display1 145 589 9
1213
For/Next LoopsHere’s an even poorer example.
Dim counter As Integer
For counter = 1 To 5counter = 1
Next counter
Since counter is set to 1 each time the loop executes it can never reach 5, so the loop is infinite.
Avoid changing the loop index!
Nested Loops
The body of a loop can contain any VB elements, including a loop.
For outerIndex = 0 To 9For innerIndex = 0 To 9‘display (Str(outerIndex) & Str(innerIndex))Next innerIndex
Next outerIndex
What is the ‘output’ from this loop?
Notes
VB allows programmers to omit the loop index from the Next statement:
For counter = start To finish‘ loop body
Next counter
Notes
This can make code difficult to follow.
For outerIndex = 0 To 9For middleIndex = 0 To 9For innerIndex = 0 To 9‘display (Str(outerIndex) & _(Str(middleIndex ) & Str(innerIndex))NextNextNext
Notes
• Proper use of indentation also helps.
For outerIndex = 0 To 9For middleIndex = 0 To 9
For innerIndex = 0 To 9 ‘display (Str(outerIndex) & _
(Str(middleIndex ) & Str(innerIndex))Next innerIndex
Next middleIndex Next outerIndex
Notes
VB allows programs to exit For - Next loops before termination is reached.
For counter = start To finishExit For
Next
Notes
Usually this command is controlled by an If statement.
For counter = start To finishIf <some other condition> Then
Exit ForEnd If
Next