2. Microsoft Visual Basic 2005 BASICS 2
Objectives
Explain what a loop is.
Use the Do While and Do Until
loops.
Use the InputBox function.
Use the Application.DoEvents
statement.
Use nested loops.
3. Microsoft Visual Basic 2005 BASICS 3
What Are Loops?
Statement repetition is accomplished by
a loop.
Iteration structure
The code required to create a loop
Loops
Do While
Do Until
For
4. Microsoft Visual Basic 2005 BASICS 4
Using the Do Loops
Knowing which of the Do loops to use is
just a matter of experience.
Both types of Do loops rely on a condition
to be either True or False.
5. Microsoft Visual Basic 2005 BASICS 5
Using Do While
Do While loop
Repeats a block of code as long as a
condition remains True
Syntax example
9. Microsoft Visual Basic 2005 BASICS 9
Choosing Do While or Do Until
Primary difference
A Do While loop tests the condition at the
top of the loop.
The Do Until loop tests the condition at
the bottom of the loop.
Do While loop
Should only be used if you are sure that
you want the loop to execute at least once
11. Microsoft Visual Basic 2005 BASICS 11
Using the InputBox Function
InputBox function
The opposite of the MsgBox function
Displays a window to ask the user for input
Using the InputBox function
Supply two strings
The text that will prompt the user
The title for the window’s title bar
13. Microsoft Visual Basic 2005 BASICS 13
Using the InputBox Function
within a Do Until Loop
The InputBox function can be used
inside a Do Until loop.
To repeatedly ask the user for data until a
specified condition is met
Place a call to the InputBox function
inside a Do Until loop.
14. Microsoft Visual Basic 2005 BASICS 14
Using the InputBox Function
within a Do Until Loop (cont.)
15. Microsoft Visual Basic 2005 BASICS 15
Potential Event Procedure
Problems and Solutions
When an event is triggered
An event procedure is executed to handle
the event
Executing an event procedure takes some
time.
Can cause problems
16. Microsoft Visual Basic 2005 BASICS 16
Event Procedure Problems
Long event procedures
Includes a loop that processes thousands
of instructions
Program is unresponsive to other events
Endless loops
Condition to stop the loop never becomes
True
Usually a programming error
18. Microsoft Visual Basic 2005 BASICS 18
Event Procedure Solutions
Application.DoEvents subroutine
Allows computer to process other events
even though the current event procedure is
not yet complete
Add it to a loop that may occupy a lot of
the computer’s time.
Introduces the potential for repetitive
Click event procedures
20. Microsoft Visual Basic 2005 BASICS 20
Event Procedure Solutions
(cont.)
Preventing repetitive Click event
procedures
Disable the button at the beginning of the
event procedure.
Prevents the user from clicking the button
again until the event processing is
completed
22. Microsoft Visual Basic 2005 BASICS 22
Using Nested Loops
Like If statements, loops may be
nested.
It is not uncommon to need a loop within
a loop.
23. Microsoft Visual Basic 2005 BASICS 23
Summary
Much of the work a computer does is
repeated many times. This repetition in
programs is accomplished using loops.
24. Microsoft Visual Basic 2005 BASICS 24
Summary (cont.)
A Do loop condition applies a test to achieve
either a True or False result. A Do While
loop tests the condition at the top of the loop
and repeats a group of statements while a
certain condition is True. A Do Until loop
tests the condition at the bottom of the loop
and repeats a group of statements until a
certain condition becomes True. The code in
a Do Until loop is always executed at least
once.
25. Microsoft Visual Basic 2005 BASICS 25
Summary (cont.)
The InputBox function creates a window
that prompts the user for input. To use the
InputBox function, you supply the text for
the prompt, the title for the window’s title
bar, and the optional default text for the
text box.
Sometimes, long event procedures can
make a program unresponsive to other
events.
26. Microsoft Visual Basic 2005 BASICS 26
Summary (cont.)
An endless loop is a loop in which the
condition that stops the loop is never met.
Clicking the Break All button in the IDE
will pause a program with an endless
loop and will highlight the code where the
program stopped.
27. Microsoft Visual Basic 2005 BASICS 27
Summary (cont.)
An Application.DoEvents subroutine
allows the program to process other
events while an event procedure is
executing.
Loops can be nested in the same way
that If statements are nested.