Documentation Center

For-Next Loop

Sax Basic provides a number of different loop structures. The loop structure allows you to ask Basic to repeat the same task for a certain number of times. The first and perhaps the most widely used of these loop structures is the For-Next loop. This structure uses a counter to determine the number of times a certain set of statements have been executed.

The general form is seen below:

For counter = startValue To endValue [Step stepValue]
  [Statements]
Next counter

The step value is optional. If no step value is provided, the counter value is incremented by one on each run through the loop. A practical example:

Sub SetReadOnly()
  Dim cnt As Integer
  Dim srclst As PslSourceList
  Set srclst = PSL.ActiveSourceList
  For cnt = 1 To srclst.StringCount
    srclst.String(cnt).State(pslStateReadOnly) = True
  Next cnt
End Sub

In this example, all strings of the active source list are set to read only. If statements, Select Case statements etc. can be nested in the loops. This way decisions can be made on each run through the loop. If for some reason you want to exit the loop based on a decision include an Exit For statement.