Поделиться через


For...Next Statement

Repeats a group of statements a specified number of times.

                      For counter = start To end [Step step]
    [statements]
    [Exit For]
    [statements]
Next

Arguments

  • counter
    Numeric variable used as a loop counter. The variable cannot be an array element or an element of a user-defined type.

  • start
    Initial value of counter.

  • end
    Final value of counter.

  • step
    Amount counter is changed each time through the loop. If not specified, step defaults to one.

  • statements
    One or more statements between For and Next that are executed the specified number of times.

Remarks

Use a For...Next structure when you want to repeat a set of statements a set number of times.

When a For…Next loop starts, Visual Basic Scripting Edition (VBScript) assigns start to counter. Before running the statement block, it compares counter to end. If counter is already past the end value, the For loop terminates and control passes to the statement following the Next statement. Otherwise, the statement block runs.

Each time VBScript encounters the Next statement, it increments counter by step and returns to the For statement. It again compares counter to end, and it again either runs the block or terminates the loop, depending on the result. This process continues until counter passes end or an Exit For statement is encountered.

The iteration values start, end, and step are evaluated only one time, before the loop starts. If your statement block changes end or step, these changes do not affect the iteration of the loop.

Note

Changing the value of counter from inside a loop can make it more difficult to read and debug your code.

You can nest For...Next loops by including one For...Next loop within another. Give each loop a unique variable name for its counter. You can also nest different kinds control structures within each other.

step Argument

The step argument can be either positive or negative. The value of the step argument determines loop processing as follows:

Value

Loop executes if

Positive or 0

counter <= end

Negative

counter >= end

If not specified, step is set to 1.

After the loop starts and all statements in the loop have executed, step is added to counter. Then, if counter is past end, the loop is exited and execution continues with the statement that follows the Next statement. Otherwise, the statements in the loop execute again.

Exit For

The Exit For statement transfers control immediately to the statement that follows the Next statement. This provides an alternative way to exit a For...Next loop. Exit For is often used after some condition is evaluated, for example, in an If...Then...Else Statement.

You can include any number of Exit For statements anywhere in a For loop.

One use of Exit For is to test for a condition that could cause an endless loop, which is a loop that could run a very large or even infinite number of times. If you detect such a condition, you can use Exit For to escape the loop.

Example

The following example illustrates the use of the For…Next statement.

Dim indexA, indexB, highB

highB = 5

For indexA = 1 to 3
    For indexB = highB to 1 Step -1
        document.write (indexB & " ")
    Next
Next

' Output:  5 4 3 2 1 5 4 3 2 1 5 4 3 2 1

Requirements

Version 1

See Also

Reference

Do...Loop Statement

Exit Statement

For Each...Next Statement

While...Wend Statement

Change History

Date

History

Reason

August 2009

Modified remarks and added example.

Information enhancement.