Top Banner
Iteration Conditional Loops Counted Loops
40

Iteration

Feb 25, 2016

Download

Documents

MANDEL

Iteration. Conditional Loops Counted Loops. Declare vars for input and result. get input. produce result from input. show result. Charting the Flow of Control. We’ve used flow charts to visualise the flow of control. The simplest form is sequence . False. True. Condition. Process. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Iteration

Iteration

Conditional Loops Counted Loops

Page 2: Iteration

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

Page 3: Iteration

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

Page 4: Iteration

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

Page 5: Iteration

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.

Page 6: Iteration

Pre-test Do UntilDo Until counter = max

step1step2step3counter = counter + 1

Loop process data step1 step2…

stop?N

Y

increment counter

Page 7: Iteration

Pre-test Do WhileDo While counter < max

step1step2step3counter = counter + 1

Loop process data step1 step2…

repeat?Y

N

increment counter

Page 8: Iteration

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.

Page 9: Iteration

Post-test Do UntilDo

step1step2step3counter = counter + 1

Loop Until counter = max

process datastep1step2…

stop?N

Y

increment counter

Page 10: Iteration

Post-test Do WhileDo

step1step2step3counter = counter + 1

Loop While counter < max

process datastep1step2…

repeat?Y

N

increment counter

Page 11: Iteration

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

Page 12: Iteration

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

Page 13: Iteration

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

Page 14: Iteration

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?_________________________________________________________

Page 15: Iteration

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.

Page 16: Iteration

For/Next LoopsAnother example._________________________________________________________

For counter = 10 To 0 Step -3‘ loop body

Next counter_________________________________________________________

What values counter will have?_________________________________________________________

Page 17: Iteration

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.

Page 18: Iteration

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.

Page 19: Iteration

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)

Page 20: Iteration

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…

Page 21: Iteration

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

Page 22: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display

Page 23: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1

Page 24: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 1

Page 25: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 14

Page 26: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 145

Page 27: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 145 5

Page 28: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 145 58

Page 29: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 145 589

Page 30: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 145 589 9

Page 31: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 145 589 9

12

Page 32: Iteration

For/Next Simulation

Dim counter As Integer

For counter = 1 To 10‘display Countercounter = counter + 3

Next counter

counter display1 145 589 9

1213

Page 33: Iteration

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

Page 34: Iteration

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!

Page 35: Iteration

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?

Page 36: Iteration

Notes

VB allows programmers to omit the loop index from the Next statement:

For counter = start To finish‘ loop body

Next counter

Page 37: Iteration

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

Page 38: Iteration

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

Page 39: Iteration

Notes

VB allows programs to exit For - Next loops before termination is reached.

For counter = start To finishExit For

Next

Page 40: Iteration

Notes

Usually this command is controlled by an If statement.

For counter = start To finishIf <some other condition> Then

Exit ForEnd If

Next