Professional Documents
Culture Documents
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
Do Loops
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
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.