400 likes | 594 Views
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.
E N D
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 Process Charting the Flow of Control • We’ve also used flow charts to map several different styles of selection. • eg.If/Then/Else
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.
N stop? Y process data step1 step2… increment counter Pre-test Do Until Do Until counter = max step1 step2 step3 counter = counter + 1 Loop
Pre-test Do While Do While counter < max step1 step2 step3 counter = counter + 1 Loop Y repeat? N process data step1 step2… 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.
process data step1 step2… increment counter N stop? Y Post-test Do Until Do step1 step2 step3 counter = counter + 1 Loop Until counter = max
process data step1 step2… increment counter Y repeat? N Post-test Do While Do step1 step2 step3 counter = counter + 1 Loop While counter < max
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
initialise loop index variable N limit exceeded? Y loop body increment loop index For/Next Loops Dim 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.
For/Next Loops In the most general case, For/Next loops use 4 integer parameters. Dim counter As Integer ‘loop index Dim start As Integer ‘initial value Dim finish As Integer ‘final value Dim increment As Integer ‘delta value Forcounter=startTofinishStepincrement ‘ loop body Next counter
For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ Forcounter= -11 To 2 Step 4 ‘ loop body Nextcounter _________________________________________________________ What values counter will have? _________________________________________________________
For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ Forcounter= -11 To 2 Step 4 ‘ loop body Nextcounter _________________________________________________________ What values counter will have? _________________________________________________________ counter will have the values -11, -7, -3, 1.
For/Next Loops Another example. _________________________________________________________ Forcounter= 10 To 0 Step -3 ‘ loop body Nextcounter _________________________________________________________ What values counter will have? _________________________________________________________
For/Next Loops Another example. _________________________________________________________ Forcounter= 10 To 0 Step -3 ‘ loop body Nextcounter _________________________________________________________ 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 loopindex must be an Integer variable. It can be declared before the loop – or not! The For statement can act as its declaration.
For/Next Loops The 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 Loops Dim counter As Integer Dim start As Integer Dim finish As Integer Dim increment As Integer For 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 Counter counter = counter + 3 Next counter Simulate this program to see what values will be displayed. skip
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter In general this is poor programming. For/Next Simulation
For/Next Loops Here’s an even poorer example. Dim counter As Integer For counter = 1 To 5 counter = 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 9 For 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 Nextcounter
Notes This can make code difficult to follow. For outerIndex = 0 To 9 For middleIndex = 0 To 9 For innerIndex = 0 To 9 ‘display (Str(outerIndex) & _ (Str(middleIndex ) & Str(innerIndex)) Next Next Next
Notes • Proper use of indentation also helps. For outerIndex = 0 To 9 For 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 finish Exit For Next
Notes Usually this command is controlled by an If statement. For counter = start To finish If <some other condition> Then Exit For End If Next