Download presentation
Presentation is loading. Please wait.
Published byAshley Floyd Modified over 9 years ago
1
Iteration Conditional Loops Counted Loops
2
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
3
Charting the Flow of Control We’ve also used flow charts to map several different styles of selection. e.g. If/Then/Else Condition TrueFalse Process
4
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
5
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. “the following steps” implies that the condition will be tested before the steps are executed. –This is called a pre-test.
6
Conditional loops In VB, loops are specified by the keywords Do and Loop. Statements that make up “the following steps” are bracketed by this pair and will be repeatedly executed. Do ' step 1 ' step 2... Loop
7
Conditional loops The keywords Until or While specify the Conditions.Note: Until counter >= Max and While counter < Max are functionally equivalent.
8
Pre-test Do Until Do Until counter = max step1 step2 step3 counter += 1 Loop process data step1 step2… stop? N Y increment counter
9
Pre-test Do While Do While counter < max step1 step2 step3 counter += 1 Loop process data step1 step2… repeat? Y N increment counter
10
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.
11
Post-test Do... Loop Until Do step1 step2 step3 counter += 1 Loop Until counter = max process data step1 step2… stop? N Y increment counter
12
Post-test Do... Loop While Do step1 step2 step3 counter += 1 Loop While counter < max process data step1 step2… repeat? Y N increment counter C
13
Repeat the following steps X times. Counted loops are so common that most programming languages have a special syntax which optimises counted loops. In VB this structure is implemented as the For/Next structure. For loopIndex = start To finish ‘ body of loop Next loopIndex
14
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. loop body limit exceeded? N Y increment loop index initialise loop index variable
15
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 For counter = start To finish Step increment ‘ loop body Next counter
16
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) will display …
17
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) will display 6.
18
For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ For counter = -11 To 2 Step 4 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________
19
For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ For counter = -11 To 2 Step 4 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________ counter will have the values -11, -7, -3, 1, and 5.
20
For/Next Loops Another example. _________________________________________________________ For counter = 10 To 0 Step -3 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________
21
For/Next Loops Another example. _________________________________________________________ For counter = 10 To 0 Step -3 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________ counter will have the values 10, 7, 4, 1 and -2.
22
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 must be declared before the loop.
23
For/Next Loops VB ignores attempts to change the loop parameters within the loop, Dim counter, start, finish, increment As Integer For counter = 1 To 5 start = -2 finish = 10 increment = 5 ‘ will have no effect Next counter except…
24
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
25
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter
26
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 1 displaycounter
27
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 11 displaycounter
28
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 4 11 displaycounter
29
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 5 4 11 displaycounter
30
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 55 4 11 displaycounter
31
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 8 55 4 11 displaycounter
32
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 9 8 55 4 11 displaycounter
33
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 99 8 55 4 11 displaycounter
34
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 12 99 8 55 4 11 displaycounter
35
For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 12 99 13 8 55 4 11 displaycounter
36
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. 12 99 13 8 55 4 11 displaycounter
37
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!
38
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?
39
Nested Loops 90 80 70 60 50 40 30 20 10 00 innerIndexouterIndex
40
Nested Loops 9190 8180 7170 6160 5150 4140 3130 2120 1110 0100 innerIndexouterIndexinnerIndexouterIndex
41
Notes VB allows programmers to omit the loop index from the Next statement: For counter = start To finish ‘ loop body Next [counter]
42
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
43
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
44
Notes VB allows programs to exit For - Next loops before termination is reached. For counter = start To finish Exit For Next
45
Notes Usually this command is controlled by an If statement. For counter = start To finish If Then Exit For End If ‘ other steps Next
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.