powerpoint 11 - Seattle Central College

Iterations (1)
To repeat a piece of code:
Up to now, need to call the procedure again
(e.g. click again on a command button)
Can also use loops:
Do Loops
Do While/Loop, Do/Loop While
Do Until/Loop, Do/Loop Until
Chapter 7
110-K1
Do While/Loop
Do While condition
The loop body is executed
as long as the condition is
true
Statements
Loop
Flow Chart
The statements must change
the value of the condition.
If not, we have a never ending
loop
True
condition
?
Statements
False
Note:
if the condition is FALSE,
the body of the loop is NOT
executed (not even once)
110-K2
Example
Draw three circles on a form
simple solution
' Create a Pen object.
Dim objPen As Pen
objPen =New Pen(Color.Red, 1)
‘Draw three circles
Me.CreateGraphics.DrawEllipse(objPen , 0, 0, 100, 100)
Me.CreateGraphics.DrawEllipse(objPen , 0, 110, 100, 100)
Me.CreateGraphics.DrawEllipse(objPen , 0, 210, 100, 100)
But what if we want a column of 10 circles?
Use a loop
110-K3
Example
Dim intCount, intYCoord As Integer
intCount = 1
Do While intCount <= 10
‘Draw a circle with new Y coordinate
Me.CreateGraphics.DrawEllipse(objPen, 0, intYCoord, 50, 5
‘update coordinates
intYCoordinate += 55
‘increment counter
intCount += 1
Loop
110-K4
What is going on?
Dim intCount, intYCoord As Integer
intCount = 1
Do While intCount < 6
‘Draw a circle with new Y coordinate
Me.CreateGraphics.DrawEllipse(objPen, 0, intYCoord, _
50, 50)
‘update coordinates
intYCoordinate += 55
‘increment counter
intCount += 1
Loop
intCount
Beginning
of the loop
End of
the loop
1
2
3
4
5
2
3
4
5
6
After the loop, intCount is 6
110-K5
Another example
Goal: Compute the time needed to double the
amount of money on a 5% interest
account
With a Do While loop
get the amount
goal = 2*amount
year = 0
5% increase
year = year + 1
amount<goal
No
Yes
110-K6
In VB
'Money Amount
Dim decAmount As Decimal
'Investment goal
Dim decGoal As Decimal
'Number of years necessary
Dim intYear As Integer
'Initialization
decAmount = CDec(txtInput.Text)
decGoal = 2*decAmount
'Loop
Do While decAmount < decGoal
decAmount = decAmount + 0.05*decAmount
intYear = intYear + 1
Loop
'Display the number of years
lblYear.Text = FormatNumber(intYear)
110-K7
Other Do Loops(1)
Do
Statements
The loop body is executed
at least once and then as
long as the condition is true.
Loop While condition
Flow Chart
Statements
condition
?
True
False
110-K8
Example
Compare:
Dim intCount As Integer
intCount = 1
Do While intCount < 1
intCount = intCount + 1
Loop
lblDisplay.Text= "intCount = “& intCount
intCount = 1
And
Dim intCount As Integer
intCount = 1
Do
intCount = intCount + 1
Loop While intCount < 1
lblDisplay.Text= "intCount = “& intCount
intCount = 2
110-K9
Other Do Loops(2)
Also: Do Until / Loop and Do / Loop Until
loop is executed as long as the
condition is FALSE
(and at least once for Do/Loop Until)
e.g. Do Until/Loop
Do Until condition
Statements
Loop
The loop body is executed
as long as the condition
is false.
Flow Chart
False
condition
?
True
Statements
110-K10
Example
Problem:
Starting from 1, how many consecutive
integers do we need to add to get a sum
greater than 200?
Dim intNumber As Integer
Dim intSum As Integer
'Compute 1+2+3+4+…
Do Until intSum>200
intNumber = intNumber + 1
intSum = intSum + intNumber
Loop
‘Display the answer
lblDisplay.Text= "intNumber = “& intNumber
110-K11
Loop Pitfalls
intCount = 1
Do Until intCount = 10
intCount = intCount + 2
Loop
What happens?
Get a never
ending loop
intCount is
never 10
intCount=1
intDoubleCount = 1
Do
intDoubleCount= intCount*2
intCount = intCount + 1
Loop Until intCount <= 10
What happens?
VB executes the
Loop only once.
Don’t confuse
While and Until
110-K12