Chapter 7 Using Loops and Timers 207
You’ve learned a handy skill that will allow you to insert a variety of useful code templates
into your own programs.
Tip To insert new snippets or reorganize the snippets you have, click the Code Snippets
Manager command on the Tools menu. The Code Snippets Manager dialog box gives you
complete control over the contents of the Insert Snippet list box and also contains a mechanism
for gathering new snippets online.
Chapter 7 Quick Reference
To Do This
Execute a group of
program statements
a specific number
of times
Insert the statements between For and Next statements in a loop. For
example:
Dim i As Integer
For i = 1 To 10
MsgBox("Press OK already!")
Next
Use a specific
sequence of numbers
with statements
Insert the statements in a For... Next loop, and use the To and Step
keywords to define the sequence of numbers. For example:
Dim i As Integer
For i = 2 To 8 Step 2
TextBox1.Text = TextBox1.Text & i
Next
Avoid an endless
Do loop
Be sure the loop has a test condition that can evaluate to False.
Declare a variable
and assign a value to
it at the same time
Use Dim to declare the variable, and then assign a value with the equal to
(=) operator. For example:
Dim Counter As Integer = 1
Exit a For... Next
loop prematurely
Use the Exit For statement. For example:
Dim InpName As String
Dim i As Integer
For i = 1 To 10
InpName = InputBox("Name?")
If InpName = "Trotsky" Then Exit For
TextBox1.Text = InpName
Next
Execute a group of
program statements
until a specific
condition is met
Insert the statements between the Do and Loop statements. For example:
Dim Query As String = ""
Do While Query <> "Yes"
Query = InputBox("Trotsky?")
If Query = "Yes" Then MsgBox("Hi")
Loop