Visual Basic - The Repetition Process
Visual Basic - The Repetition Process
The Repetition
Process in
Visual Basic
Event Driven L
oops
Determinate Lo
ops
Chapter 5:
Indeterminate L
oops
The Repetition Process in
Sequential Acc
Visual Basic
ess Files
Combo Boxes
Executable File
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 1
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
The Repetition Process
Event Driven L
oops • The capability to repeat one or more
statements as many times as necessary is
Determinate Lo
ops what really sets a computer apart from
other devices
Indeterminate L
oops • All loops have two parts:
Sequential Acc
– the body of the loop (the statements being
ess Files repeated)
– a termination condition that stops the loop
Combo Boxes
• Failure to have a valid termination
Executable File
s condition can lead to an endless loop
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 2
Wiley. All rights 0 by McKeown and Piercy
reserved.
Types of Loops
Chapter 5:
The Repetition
Process in
Visual Basic
• There are three types of loops
Event Driven L
oops – event-driven
– determinate
Determinate Lo
ops – indeterminate
• Event-driven loops are repeated by the user
Indeterminate L
oops causing an event to occur
• Determinate loops repeat a known number
Sequential Acc
ess Files of times
• Indeterminate loops repeat an unknown
Combo Boxes
number of times
Executable File • Variables should be initialized before being
s
used in a loop
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 3
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Event-Driven Loops
Visual Basic
Determinate Lo
ops
Indeterminate L
oops
Sequential Acc
ess Files
Combo Boxes
Executable File
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 5
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
Use of AddItem and Clear Methods
Event Driven L
oops • The AddItem method is used to add
Determinate Lo
items to a list box at run time
ops
• Form of AddItem method
Indeterminate L
oops list1.Additem string
Sequential Acc • Always add a string to list box since it
ess Files
contains text
Combo Boxes
• The Clear method clears a list box
Executable File
s
list1.Clear
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 6
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in Code to Sum a Series of Numbers
Visual Basic
Event Driven L
oops
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 7
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Determinate Loops Using For-
Visual Basic
Next Loop
Event Driven L
oops Best way to create a determinate loop is to use a For-
Next Loop
Determinate Lo
ops
Form of For-Next Loop:
For variable = start value to end value Step
Indeterminate L
change value
oops statements that compose body of loop
Next variable
Sequential Acc where variable = the counter variable in the loop
ess Files
start value = the beginning value of the counter variable
Combo Boxes
end value = the ending value of the counter variable
change value = the amount the counter variable changes
Executable File
each time through the loop
s
Next variable = the end of the For loop
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 8
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic Example of For-Next Loop
Event Driven L
oops
Determinate Lo
ops
Beginning Ending Change
Counter Value Value
Variable Value
Indeterminate L
oops
For Statement For intCounter = 1 to 10 Step 1
intSum = intSum + intCounter
Sequential Acc
ess Files Next intCounter
Next Statement Body of Loop
Combo Boxes
Executable File
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 9
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Code To Sum using For-Next Loop
Visual Basic
Private Sub cmdCalc_Click()
Event Driven L Dim intTheValue as Integer, intSum as Integer
oops
Dim intNumValues as Integer, intCounter as Integer
intSum = 0 ‘Initialize Sum to Zero
If txtNumValues.Text = "" Then ’Number not entered
Determinate Lo
ops Msgbox "Please enter number of values to be summed“
Exit Sub ’Do not go into loop
End if
Indeterminate L intNumValues = CInt(txtNumValues.Text)
oops For intCounter = 1 to intNumValues
intTheValue = CInt(InputBox("Enter next value"))
intSum = intSum + intTheValue
Sequential Acc
ess Files lstEntries.AddItem Str(intTheValue) ‘Add to list
Next
txtSum.Text = Str(intSum)
Combo Boxes lstEntries.AddItem "Sum is " & Str(intSum)
End Sub
Executable File
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 10
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
Printing a List
Event Driven L
oops • To print the contents of listbox, use the Listcount
and List() properties of the list box
Determinate Lo
ops • Listcount is the number of items in list box but List()
property runs from 0 to Listcount -1
Indeterminate L
oops
• List(intCounter) is equal to the contents of the
corresponding list box
Sequential Acc
ess Files
• Code to print contents of list box to Immediate
Window:
Combo Boxes For intCounter = 0 to lstEntries.ListCount - 1
Debug.Print lstEntries.List(intCounter)
Executable File Next
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 11
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
Indeterminate Loops
Event Driven L • Indeterminate loops run for an unknown number of
oops
repetitions until a condition is true or while a
Determinate Lo
condition is true
ops
• Four types of indeterminate loops
– Until loop with termination condition before body of loop
Indeterminate L
oops – While loop with termination condition before body of loop
– Until loop with termination condition after body of loop
Sequential Acc
ess Files – While loop with termination condition after body of loop
• Pre-Test loops have termination condition before
Combo Boxes loop body
Executable File
• Post-test loops have termination condition after loop
s body
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 12
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in Form of Pre- and Post-Test Loops
Visual Basic
Event Driven L
oops
• The form of the pre-test loops is:
Determinate Lo
ops
Do Until (or While) condition
body of loop
Indeterminate L
oops Loop
Sequential Acc
• The form of the post-test loops is:
ess Files
Do
Combo Boxes body of loop
Executable File
Loop Until (or While) condition
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 13
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Pre and Post-Test Loops
Visual Basic
Do While Loop
Event Driven L
oops True Condition (loop continues) False Condition (loop ends)
Do While condition Do While condition
Determinate Lo statement1 statement1
ops statement2 statement2
(continued) (continued)
Indeterminate L Loop Loop
oops next statement next statement
Do Until Loop
Sequential Acc
ess Files True Condition (loop ends) False Condition (loop continues)
Do Until condition Do Until condition
statement1 statement1
Combo Boxes
statement2 statement2
(continued) (continued)
Executable File Loop Loop
s
next statement next statement
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 14
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
Processing an Unknown Number of Values
from a File
Event Driven L
oops
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 15
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
Using Sequential Access Files
Event Driven L
oops
• Sequential access files must be read in same order as they
Determinate Lo
are created so they replicate the action of entering data from a
ops keyboard.
• The number of records on a sequential access file is often
Indeterminate L unknown, but there is an invisible binary marker at the end of
oops
the file called the EOF (end of file) marker.
• Use a Do While loop or a Do Until loop to input data from
Sequential Acc
ess Files sequential access file.
• Loops can input data until the EOF marker is encountered (or
Combo Boxes while it has not been encountered).
• Create sequential access files by using a text editor such as
Executable File Notepad.
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 16
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in Opening and Inputting Data from a File
Visual Basic
Event Driven L
oops
• Files must be opened with the Open statement:
Determinate Lo Open “filename" for Input as #n
ops
• To input data from a file, use the Input #n,
Indeterminate L statement
oops
Input #n, list of variables
Sequential Acc
• Files must be closed at end of procedure
ess Files
Close #n
Combo Boxes
• Using an Until loop to input data from file
Do Until EOF(n)
Executable File Input #n, list of variables
s
Loop
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 17
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition Code to Input and Sum values from File
Process in
Visual Basic
Private Sub cmdCalc_Click()
Event Driven L Dim intTheValue As Integer, intSum As Integer
oops Dim intNumValues As Integer
Open "a:\SumData.txt" For Input As #10
Determinate Lo intSum = 0
ops
intNumValues = 0
Do Until EOF(10) ’Input to end of file
Indeterminate L Input #10, intTheValue
oops
intSum = intSum + intTheValue
intNumValues = intNumValues + 1
Sequential Acc
ess Files lstEntries.AddItem Str(intTheValue)
Loop
Combo Boxes
txtNumValues.Text = Str(intNumValues)
txtSum.Text = Str(intSum)
lstEntries.AddItem "Sum is " & str(intSum)
Executable File
s Close #10
End Sub
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 18
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
The Combo Box
Event Driven L • A combo box is a combination of a text box and a
oops
list box.
Determinate Lo • It has a text box portion that is displayed at all
ops
times and a drop-down list of items that can be
Indeterminate L
displayed by clicking on the down arrow.
oops
• One property of note for the combo box is the
Sequential Acc
Style property, which can be set at design time to
ess Files Drop Down combo (the default), Simple Combo, or
Drop Down list. Its prefix is cbo .
Combo Boxes
• The combo box has all the properties and methods
Executable File of the list box including the AddItem, ListCount,
s
and List() properties.
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 19
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic More on Combo Boxes
Event Driven L
oops
• The default event for the Combo box is the Change
Determinate Lo
ops
event--to use the Click event, you must change to it.
• The Sorted property is a useful property for the combo
Indeterminate L
oops
and list boxes that arranges the items in the box
alphabetically.
Sequential Acc • Items can be removed from a combo or list box with
ess Files
the RemoveItem method which requires that number
Combo Boxes
of the item to be remove be given. To remove a
selected item, you can use the ListIndex property, eg,
Executable File cboMembers.RemoveItem cboMembers.ListIndex
s
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 20
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in Nested Loops
Visual Basic
Event Driven L • A Nested loop is a loop within a loop. Must complete the
oops
inner loop within the outer loop.
• Nested For-Next loops have a For-Next loop within a For-
Determinate Lo
ops Next loop. The inner loop will go through all its values for
each value of the outer loop.
Indeterminate L • Three key programming rules to remember about using
oops nested For-Next loops:
• Always use different counter variables for the outer
Sequential Acc and inner For-Next loops.
ess Files
• Always have the Next statement for the inner For-
Combo Boxes Next loop before the Next statement for the outer
For-Next loop.
Executable File • Always include the counter variable in the Next
s
statements to distinguish between the loops.
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 21
Wiley. All rights 0 by McKeown and Piercy
reserved.
Chapter 5:
The Repetition
Process in
Visual Basic
Creating an Executable File
Event Driven L
oops • An executable file is one that can be
executed on any computer on which it is
Determinate Lo
ops installed.
• Creating an executable file in VB is
Indeterminate L
oops accomplished with the File|Make
filename.exe menu selection.
Sequential Acc
ess Files • Once an executable file is created, a
shortcut to it can be created by right-
Combo Boxes clicking the file name in Windows Explorer
and selecting Create Shortcut.
Executable File
s • Drag the shortcut to the desktop.
Copyright © 2001 by
Introduction to Programming with Visual Basic 6. 22
Wiley. All rights 0 by McKeown and Piercy
reserved.
Debugging Loops
Chapter 5:
The Repetition
Process in
Visual Basic