For...Next statement

Usage

The following code displays and Input Box asking how many beeps. The code will then make the computer beep the specified number of times. The code in bold may be ommitted.

Dim NumBeeps As Integer
NumBeeps = InputBox("How many beeps?")
For Counter = 1 To NumBeeps Step 1
    Beep '// beep
Next Counter

The following code counts down from 2000.

For Counter = 2000 To 1 Step -1 '// This counts down from 2000 to 1
    Text1.Text = Counter
    Text1.Refresh '// Update Text1
Next Counter
Msgbox "Count down finished"

To exit a For...Next you can use the statement Exit For. When in a loop, you can also change the value of the Counter variable, so that the For...Next will miss some values:

For Counter = 1 To 1000 Step 1
    '// skip any values between 100 and 600
    If Counter = 100 Then Counter = 601
    '// set text value
    Text1.Text = Counter
    Text1.Refresh '// Update Text1
Next Counter

Using this code, you will notice that when the text box is counting up, it will miss all numbers between 100 and 600

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay