Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Loops Counted Loops

Similar presentations


Presentation on theme: "Conditional Loops Counted Loops"— Presentation transcript:

1 Conditional Loops Counted Loops
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 True False 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
process data step1 step2… stop? N Y increment counter Do Until counter = max step1 step2 step3 counter += 1 Loop

9 Pre-test Do While Do While counter < max step1 step2 step3
Loop Y repeat? N process data step1 step2… 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
step1 step2 step3 counter += 1 Loop Until counter = max process data step1 step2… stop? N Y increment counter

12 Post-test Do ... Loop While
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
limit exceeded? N Y increment loop index initialise loop index variable 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.

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 For counter = -11 To 2 Step 4 ‘ loop body Next counter
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 For counter = -11 To 2 Step 4 ‘ loop body Next counter
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 For counter = 10 To 0 Step -3 ‘ loop body Next counter
Another example. _________________________________________________________ For counter = 10 To 0 Step -3 ‘ loop body Next counter What values will counter have?

21 For/Next Loops For counter = 10 To 0 Step -3 ‘ loop body Next counter
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 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter display counter

26 For/Next Simulation 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 1 display counter

27 For/Next Simulation 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 1 display counter

28 For/Next Simulation 4 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 4 1 display counter

29 For/Next Simulation 5 4 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 5 4 1 display counter

30 For/Next Simulation 5 4 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 5 4 1 display counter

31 For/Next Simulation 8 5 4 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 8 5 4 1 display counter

32 For/Next Simulation 9 8 5 4 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 9 8 5 4 1 display counter

33 For/Next Simulation 9 8 5 4 1 display counter Dim counter As Integer
For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 9 8 5 4 1 display counter

34 For/Next Simulation 12 9 8 5 4 1 display counter
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 12 9 8 5 4 1 display counter

35 For/Next Simulation 12 9 13 8 5 4 1 display counter
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 12 9 13 8 5 4 1 display counter

36 For/Next Simulation 12 9 13 8 5 4 1 display counter
Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter In general this is poor programming. 12 9 13 8 5 4 1 display counter

37 Avoid changing the loop index!
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 9 8 7 6 5 4 3 2 1 innerIndex outerIndex

40 Nested Loops 9 1 8 7 6 5 4 3 2 innerIndex outerIndex

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 <some other condition> Then Exit For End If ‘ other steps Next


Download ppt "Conditional Loops Counted Loops"

Similar presentations


Ads by Google