Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 38,112 times

Contents

Related Categories

For...Next statement - Usage

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Posted by davour on 01 Nov 2003

    [quote]I need an equation such as a * b = c and a / c = b and b / c = a where the user types in 2 of the variables in text boxes and some proccess (such as a button click) would calculate the result....

  • Posted by mrsmiff@mac.com on 30 Oct 2001

    I need to create an If...Then loop because I need an equation such as a * b = c and a / c = b and b / c = a where the user types in 2 of the variables in text boxes and some proccess (such as a butto...

  • Posted by James Crowley on 29 Oct 2001

    What exactly do you mean by a circular reference...? (I know what you mean in reference to Excel, but I'm not sure how'd that translate to another language...)

  • Stupid Question (I think)

    Posted by mrsmiff@mac.com on 24 Oct 2001

    Is it possible to create a circular reference (in any programming language), because I know that Excel won't do it?