Home > Outline & Objectives
1
Chapter
6
Repetition
2
Outline
& Objectives
3
Types of
LOOP Structures
4
Basic Definition
5
Basic Components
of Loops
6
The Do While
����. Loop
Do While condition is true
statement(s)
Loop
7
Flowchart
for a Do While Loop
Is
the condition true
Execute statements
within
the
loop
Execute statements
that
follow the loop
Yes
No
8
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
9
The Do While
����. Loop
10
Controlling
Loops
11
Example
of event-controlled loops
passWord = ""
Do While passWord <> "SHAZAM"
passWord = UCase(InputBox("What is the password?"))
Loop
12
Counter-controlled
Loops
13
Example
num = 1
Do While num <= 10
picOutput.Print num;
num = num + 1
Loop
14
Do Until
����. Loop
15
Example (requires the user
to give a password before opening a file)
Private Sub cmdDisplay_Click()
Dim passWord As String, info As String
If UCase(txtName.Text) = "SECRET.TXT" Then
Do
passWord = UCase(InputBox("What is the password?"))
Loop Until passWord = "SHAZAM"
End If
Open txtName.Text For Input As #1
Input #1, info
picItem.Cls
picItem.Print info
Close #1
End Sub
16
Example (years to deplete
a saving account)
Private Sub cmdEstimate_Click()
Dim amt As Single, yrs As Integer
' Years to deplete savings account
picResult.Cls
amt = 15000
yrs = 0
Do
amt = amt * 1.05 - 1000
yrs = yrs + 1
Loop Until amt <= 0
picResult.Print "It takes"; yrs; "years to deplete the account."
End Sub
17
Comparing
While�� and Until Loops
18
EOF Function
Open ��PHONE.TXT�� for Input As #1
Do While Not EOF(1)
Input #1, nom, phoneNum
picOutput.Print nom, phoneNum
Loop
Close #1
19
Counters
and Accumulators
20
Example: Counter & Accumulator
Private Sub cmdAnalyze_Click()
Dim numCoins As Integer, sum As Single, value As Single
Open "COINS.TXT" For Input As #1
numCoins = 0
sum = 0
Do While Not EOF(1)
Input #1, value
numCoins = numCoins + 1
sum = sum + value
Loop
picValue.Print "The value of the"; numCoins; "coins is"; sum; "cents."
End Sub
Compare
21
Review
num = 11
Do While num <= 10
picOutput.Print num;
num = num + 1
Loop
22
num = 11
Do
picOutput.Print num;
num = num + 1
Loop until num
<= 10
How many times will the following loops execute?
Review
Do While i < 10
i = i + 1
Loop
23
Do
i = i + 1
Loop While i < 10
Which loop
is infinite?
Do Until i < 10
Loop
Do
i = i + 10
Loop Until i < 10
24
For ��
Next Loop
For controlVariable = initial To terminal
statement(s)
Next controlVariable
25
Example
Private Sub cmdDisplayTable_Click()
Dim i As Integer
��Display a table of the first 5 numbers and their squares
For i = 1 To 5
picTable.Print i; i ^ 2
Next i
End Sub
Initial Value
Control variable
Terminating value
26
Example
Dim numVar As Integer
For numVar = 1 To 5 Step 2
picOutput.Print numVar;
Next numVar
Output: 1 3 5
27
When a For
statement is encountered
28
Rules for
Using For
... Next
loop
29
Example
Private Sub cmdDisplay_Click()
Dim i As Integer
For i = 1 To 10
picOutput.Print "*";
Next i
End Sub
Output: **********
30
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
31
Example
Dim numVar As Integer
For numVar = 8 To 1 Step -2
picOutput.Print numVar;
Next numVar
Output: 8 6 4 2
32
Nested Loops
For outer = 1 To 4
For inner = 1 To 2
..
..
Next inner
Next outer
33
Example: Display
a 10x10 rectangle of stars
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 10
For j = 1 To 10
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
Review
For i= -5 To -1 Step - 2
picOutput.Print i;
Next i
picOutput.Print i;
34
For i= 1 To 5 Step 2
i=i+1
picOutput.Print i;
Next i
picOutput.Print i;
35
Review
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 10
For j = i To 10
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer
For i = 1 To 10
For j = 1 To i
picOutput.Print "*";
Next j
picOutput.Print
Next i
End Sub
All Rights Reserved Powered by Free Document Search and Download
Copyright © 2011