For...Next

Syntax

For CounterVariable = Start To End [Step Increment]
Series of instructions
Next [CounterVariable]

Remarks

Repeats the series of instructions between For and Next while increasing CounterVariable by 1 (default) or Increment until CounterVariable is greater than End. If Start is greater than End, Increment must be a negative value; CounterVariable decreases by Increment until it is less than End.

If you place one or more For¼Next loops within another, use a unique CounterVariable for each loop, as in the following instructions:


For I = 1 To 10
    For J = 1 To 10
        For K = 1 To 10
            'Series of instructions
        Next K
    Next J
Next I

For more information about For¼Next, see Chapter 4, "Advanced WordBasic," in Part 1, "Learning WordBasic."

Examples

This example displays five message boxes in a row, each giving the current value of count:


For count = 1 To 5
    MsgBox "Current value of count is" + Str$(count)
Next count

The following example produces exactly the same effect as the previous example by decrementing the value of count in steps of –1:


For count = 5 To 1 Step -1
    MsgBox "Current value of count is" + Str$(count)
Next

The following example demonstrates how you can use WordBasic counting functions with a For¼Next loop to perform an operation on all the items in a certain category. In this example, the names of all the bookmarks defined in the active document are stored in the array mark$().


numBookmarks = CountBookmarks()
arraySize = numBookmarks - 1
Dim mark$(arraySize)
For n = 0 To arraySize
    mark$(n) = BookmarkName$(n + 1)
Next

See Also

Goto, If¼Then¼Else, Select Case, While¼Wend