1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Iteration Conditional Loops Counted Loops. Charting the Flow of Control We’ve used flow charts to...
-
Upload
ashley-floyd -
Category
Documents
-
view
219 -
download
0
Transcript of Iteration Conditional Loops Counted Loops. Charting the Flow of Control We’ve used flow charts to...
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.
• e.g.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.
• “the following steps” implies that the condition will be tested before the steps are executed. – This is called a pre-test.
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
Conditional loops
The keywords Until or While specify the Conditions.
Note:Note:
Until counter >= Max
and
While counter < Max
are functionally equivalent.
Pre-test Do Until
Do Until counter = max
step1
step2
step3
counter += 1
Loop process data step1 step2…
stop?N
Y
increment counter
Pre-test Do While
Do While counter < max
step1
step2
step3
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 ... Loop Until
Do
step1
step2
step3
counter += 1
Loop Until counter = max
process datastep1step2…
stop?N
Y
increment counter
Post-test Do ... Loop While
Do
step1
step2
step3
counter += 1
Loop While counter < max
process datastep1step2…
repeat?Y
N
increment counter
C
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
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
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
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 …
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 .
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?_________________________________________________________
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.
For/Next Loops
Another example._________________________________________________________
For counter = 10 To 0 Step -3‘ loop body
Next counter_________________________________________________________
What values will counter have?_________________________________________________________
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.
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.
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…
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 see what values will be displayed.
skip
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10
‘display Counter
counter = counter + 3
Next counter
displaycounter
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10
‘display Counter
counter = counter + 3
Next counter
1
displaycounter
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10
‘display Counter
counter = counter + 3
Next counter
11
displaycounter
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10
‘display Counter
counter = counter + 3
Next counter
4
11
displaycounter
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10
‘display Counter
counter = counter + 3
Next counter
5
4
11
displaycounter
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10
‘display Counter
counter = counter + 3
Next counter
55
4
11
displaycounter
For/Next Simulation
Dim counter As Integer
For counter = 1 To 10
‘display Counter
counter = counter + 3
Next counter8
55
4
11
displaycounter
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
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
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
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
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
For/Next Loops
Here’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 9
For innerIndex = 0 To 9
‘display (Str(outerIndex) & Str(innerIndex))
Next innerIndex
Next outerIndex
What is the ‘output’ from this loop?
Nested Loops
9190
8180
7170
6160
5150
4140
3130
2120
1110
0100
innerIndexouterIndexinnerIndexouterIndex
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 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