Library articles and tutorials

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

AddThis

Comments

  1. 01 Nov 2003 at 09:09

    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.  


    first, I know what you mean by circular reference but in the way excel means it is that a circular reference is a feedback loop... and any programming that allows this usualy crashes


    So... lets see what you want to perform.


    we are creating the value of "C" from both "A" and "B"
    we are creating the value of "B" from both "A" and "C"
    we are creating the value of "A" from both "B" and "C"


    It cant work


    you cant create a value from nothing - to make anything you must add something to it to create it


    ie. think of it this way - your mum and dad created you, thats an equation
    if Mum = A and Dad = B and You are C then
    A + B = C


    Ok, so... now this is just an equation and dont get all religious on me but lets say you wanted to do the following equation


    Mum + Son = Dad
    or Son + Dad = Mum


    Since Son is a product of Mum and Dad you cant use that product to create its source


    Why would you need this equation, im curious?


  2. 30 Oct 2001 at 08:39

    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 button click) would calculate the result.  At the moment I keep getting the syntax wrong (am quite new to programing) so it never works

  3. 29 Oct 2001 at 16:15

    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...)

  4. 24 Oct 2001 at 15:18

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

Leave a comment

Sign in or Join us (it's free).

Related discussion