You are on page 1of 4

For - Next

Dim i As IntegerFor i = 1 To 5 Step 1MsgBox("Your position is "


& i)Next i----------------------------------------------------Dim i As
Integer For i = 1 To 4 Beep() Next i

Exit For Statement

Visual Basic allows for this possibility by providing the Exit For
statement, which you can use to terminate the execution of a
For . . . Next loop early and move execution to the first
statement after the loop

Dim i As Integer Dim InpName As String For i = 1 To 10


InpName = InputBox("Enter your name or type Done to quit.")
If InpName = "Done" Then Exit For TextBox1.Text = InpName
Next i

Do Loops

1.Do While condition

For example, the following Do loop prompts the user for input
and displays that input in a text box until the word Done is
typed in the input box:
Dim InpName As String Do While InpName <> "Done"
InpName = InputBox("Enter your name or type Done to quit.")
If InpName <> "Done" Then TextBox1.Text = InpName Loop

The conditional statement in this loop is InpName <> "Done",


which the Visual Basic compiler translates to mean “loop so
long as the InpName variable doesn’t contain the exact word
‘Done’” This brings up an interesting fact about Do loops: If the
condition at the top of the loop isn’t True when the Do
statement is first evaluated, the Do loop is never executed
Here, if the InpName string variable did contain the “Done”
value before the loop started (perhaps from an earlier
assignment in the event procedure), Visual Basic would skip the
loop altogether and continue with the line below the Loop
keyword If you always want the loop to run at least once in a
program, put the conditional test at the bottom of the loop For
example, the loop:

Dim InpName As String Do InpName = InputBox("Enter your


name or type Done to quit.") If InpName <> "Done" Then
TextBox1.Text = InpName Loop While InpName <> "Done"

Is essentially the same as the previous Do loop, but here the


loop condition is tested after a name is received from the
InputBox function This has the advantage of updating the
InpName variable before the conditional test in the loop so that
a preexisting Done value won’t cause the loop to be skipped
Testing the loop condition at the bottom ensures that your loop
is executed at least once, but often it forces you to add a few
extra statements to process the data.

2.Do Loop While

Because of the relentless nature of Do loops, it’s very important


to design your test conditions so that each loop has a true exit
point If a loop test never evaluates to False, the loop executes
endlessly, and your program might not respond to input
Consider the following example:

Dim Number as Double Do Number = InputBox("Enter a


number to square. Type –1 to quit.") Number = Number *
Number TextBox1.Text = Number Loop While Number >= 0

In this loop, the user enters number after number, and the
program squares each numberand displays it in the text box
Unfortunately, when the user has had enough, he or shecan’t
quit because the advertised exit condition doesn’t work When
the user enters –1, theprogram squares it, and the Number
variable is assigned the value 1 (The problem can befixed by
setting a different exit condition The next example
demonstrates how to checkif the user clicked the Cancel button
and exited the loop ) Watching for endless loops isessential
when you’re writing Do loops Fortunately, they’re pretty easy
to spot if you test your programs thoroughly.

3.Do Loop Until

You might also like