Transcript Ch6

Chapter 6
Repetition
Chapter 6 - Visual Basic
Schneider
1
Outline & Objectives



Loop Structure
Elements of a Loop Structure
Processing Lists of Data with Do Loops
Chapter 6 - Visual Basic
Schneider
2
Types of LOOP Structures



Do While ……. Loop
Do Until …… Loop
For …… Next loop
Chapter 6 - Visual Basic
Schneider
3
Basic Definition

Looping: the process of repeating a series
of statements multiple times until a
criteria is met
Chapter 6 - Visual Basic
Schneider
4
Basic Components of Loops


Loop control variable: A variable used to
determine whether a loop will be
executed
Loop body: The statement (s) that are
executed each time a loop repeats
Chapter 6 - Visual Basic
Schneider
5
The Do While ……. Loop
Do While condition is true
statement(s)
Loop
Chapter 6 - Visual Basic
Schneider
6
Flowchart for a Do While Loop
No
Is the condition true
Yes
Execute statements
within
the loop
Execute statements
that follow the loop
Chapter 6 - Visual Basic
Schneider
7
Example
(Displays the numbers from 1 through 10)
Private Sub cmdDisplay_Click()
Dim num As Integer
' Display the numbers from 1 to 10
num = 1
Do While num <= 10
picNumbers.Print num;
num = num + 1
Loop
End Sub
Output: 1 2 3 4 5 6 7 8 9 10
Chapter 6 - Visual Basic
Schneider
8
The Do While ……. Loop


Is executed as long as the condition is True.
If condition is False then the next statement
after the Loop is executed.
Chapter 6 - Visual Basic
Schneider
9
Controlling Loops

Methods of controlling loops:
• Counter-controlled loops
• repeat a specific number of times
• Event-controlled loops
• repeat until something happens in the loop body to
change the value of loop control variable.
Chapter 6 - Visual Basic
Schneider
10
Example of event-controlled loops
passWord = ""
Do While passWord <> “ADMIN"
passWord = UCase(InputBox("What is the password?"))
Loop
Chapter 6 - Visual Basic
Schneider
11
Counter-controlled Loops



Is useful when the programmer knows how many
times the loop should be executed.
Initialize the counter by setting it to a
beginning value before entering the loop.
The counter is incremented (or decremented)
by the same value during each repetition.
Chapter 6 - Visual Basic
Schneider
12
Example for Counter-controlled loop
num = 1
Do While num <= 10
picOutput.Print num;
num = num + 1
Loop
Output: 1 2 3 4 5 6 7 8 9 10
Chapter 6 - Visual Basic
Schneider
13
Example
Example
Dim x As Integer
Dim s As Integer
Do While x >= 2
x = x + 1
s = s + x
Loop
picOutput.Print x; s
Example
Example
Dim x As Integer
Dim s As Integer
Do
x = x + 1
s = s + x
Loop While x >= 2
picOutput.Print x; s
Do Until ……. Loop


Is executed until the condition becomes True
Any Do While…. Loop can be rewritten as a Do
Until ….. Loop
Chapter 6 - Visual Basic
Schneider
18
Example
Dim x As Integer
Dim s As Integer
Do
x = x + 1
s = s + x
Loop Until x >= 2
picOutput.Print x; s
Example
Dim x As Integer
Dim s As Integer
Do
x = x + 1
s = s + x
Loop Until x <= 2
picOutput.Print x; s
Example
Dim x As Integer
Dim s As Integer
Do Until x >= 2
x = x + 1
s = s + x
Loop
picOutput.Print x; s
Comparing While… and Until Loops



The Do While … Loop executes while the
condition is true
The Do Until….. Loop executes until the
condition is true
Both can be used to create any type of loop
Chapter 6 - Visual Basic
Schneider
22
Converting While loop to Until loop
Do While Condition
Action(s)
Equivalents to
Loop
Do While x <= y
Action(s)
Loop
Equivalents to
Do Until Not(Condition)
Action(s)
Loop
Do Until x > y
Action(s)
Loop
Do While x <=3 and x >=10
Do Until Not(x<=3 and x>=10)
Action(s)
Action(s)
Loop
Equivalents to Loop
Review
How many times will the following loops
execute?
num = 11
Do While num <= 10
picOutput.Print num;
num = num + 1
Loop
num = 11
Do
picOutput.Print num;
num = num + 1
Loop until num <= 10
0
Infinite loop
Chapter 6 - Visual Basic
Schneider
24
Review
Which loop is infinite?
i = 1
Do While i < 10
i = i + 1
Loop
i = 0
Do
i = i + 1
Loop While i < 10
NO
NO
i = 11
Do Until i < 10
Loop
Yes
i = 0
Do
i = i + 10
Loop Until i < 10
Yes
EOF Function

EOF(n) is True if the end of the file having reference
number n has been reached. Otherwise, it is False
Chapter 6 - Visual Basic
Schneider
26
Example
Dim count As Integer
Open “DATA.TXT” For Input
As #1
Do While NOT EOF(1)
Input #1, x
count= count + 1
s=s+x
Loop
Print count;s;x
will be true if the end
of file has been
reached, and false
otherwise
5 150 50
27
Counters and Accumulators


A counter is a numeric variable that keeps track
of the number of items that have been
processed in a loop.
An accumulator is a numeric variable that holds
a sub-total during multiple passes through a
loop.
Chapter 6 - Visual Basic
Schneider
28
Example: Counter & Accumulator
Private Sub cmdAnalyze_Click()
Dim numCoins As Integer, sum As Single, value As
Single
COINS.TXT
Open "COINS.TXT" For Input As #1
numCoins = 0
50
sum = 0
10
Do While Not EOF(1)
5
Input #1, value
numCoins = numCoins + 1
25
sum = sum + value
Loop
picValue.Print "The value of the"; numCoins; "coins is";
sum; "cents."
End Sub
Chapter 6 - Visual Basic
Schneider
29
Compare




Do While ……. Loop
Do ……. Loop While
Do ……. Loop Until
Do Until ……. Loop
Chapter 6 - Visual Basic
Schneider
30
For … Next Loop
A loop where the number of iterations is
determined by a range of values for a
numeric variable
 Syntax:
For controlVariable = initial To terminal
statement(s)
Next controlVariable

Chapter 6 - Visual Basic
Schneider
31
Example
Private Sub cmdDisplayTable_Click()
Dim i As Integer
‘Display a table of the first 5 numbers and their
squares
Control variable
For i = 1 To 5
Terminating value
picTable.Print i; i ^ 2
Initial Value
Next i
End Sub
Chapter 6 - Visual Basic
Schneider
32
Example
Dim numVar As Integer
For numVar = 1 To 5 Step 2
picOutput.Print numVar;
Next numVar
Output: 1 3 5
Chapter 6 - Visual Basic
Schneider
33
When a For statement is
encountered
1.
2.
3.
The control variable is assigned the
initial value.
After each loop iteration, the step value
is added to the value of the control
variable. (If there is no step value, 1 is
added.)
Iteration continues until the terminating
value is exceeded.
Chapter 6 - Visual Basic
Schneider
34
Rules for Using For ... Next loop


You should never modify the value of the
loop control variable in the loop body.
Each For loop must end with a Next
statement.
Chapter 6 - Visual Basic
Schneider
35
Example
Private Sub cmdDisplay_Click()
Dim i As Integer
For i = 1 To 10
picOutput.Print "*";
Next i
End Sub
Output: **********
Chapter 6 - Visual Basic
Schneider
36
Example
Private Sub cmdDisplay_Click()
Dim i As Integer, stars As Integer
stars = Val(InputBox("Row length (1-20) :"))
For i = 1 To stars
picOutput.Print "*";
Next i
End Sub
Chapter 6 - Visual Basic
Schneider
37
Example
Dim numVar As Integer
For numVar = 8 To 1 Step -2
picOutput.Print numVar;
Next numVar
Output: 8 6 4 2
Chapter 6 - Visual Basic
Schneider
38
which of the following statements is true
about the following code
For i = 1 To 5.8 Step 1
pic1.Print i;
Next I




This code will print numbers from 1 to 6
This code will print numbers from 1 to 5
Run-time error
This code will print numbers from 1 to 5.8
What is the output of the following
code?
Dim i As Integer
For i = 9 To 7
Print i;
Next i
Nothing will be printed
What is the output of the following
code?
Dim i as integer
i=4
for i=3 To 5
print 2*i;
Next i
print i
6 8 10 6
Example
Dim x as integer
x=0
for x=3 To 1
print x;
Next x
print x
3
How many stars (*) are printed when the
following code is executed?
Dim i as Integer
for i = 1 To 6 Step 1
print "*"
i=i+2
next i
2 stars
Private Sub Command1_Click()
For x = 1 To 3 Step 0
Print x;
Next
Print " finally "; x
End Sub
Infinite loop
Nested Loops
For outer = 1 To 4
For inner = 1 To 2
..
..
Next inner
Next outer
Chapter 6 - Visual Basic
Schneider
46
Example: Display a 3x3 rectangle of
stars
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 3
For j = 1 To 3
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
Chapter 6 - Visual Basic
Schneider
***
***
***
47
Review
For i= -5 To -1 Step - 2
257
picOutput.Print i;
Next i
For i= 1 To 5 Step
picOutput.Print i;
2
i=i+1
-5
picOutput.Print
i;
Next i
Chapter 6 - Visual Basic
Schneider
48
picOutput.Print i;
Review
***
**
*
*
**
***
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 3
For j = i To 3
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
Chapter 6 - Visual Basic
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 3
For j = 1 To i
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
Schneider
49
How many times the statement print
"Good Luck" will be executed
Dim i as integer, j as integer
for i=0 To 3
for j=0 To 0
print "Good Luck"
Next j
Next i
4