Equinox Online Help - Language Reference - A to Z

Home

For...Next

Applies to
Syntax
For CounterName = StartExpression To EndExpression [Step IncrementExpression]
statements
[Exit For]
[Continue For]
Next [CounterName]
Action[Statement] The enclosed statements repeat a fixed number of times.
ScopeUsable anywhere.
Notes

The For statement generates a set of values for the variable CounterName. The first value in this set is StartExpression. Each subsequent value is calculated by adding IncrementExpression until EndExpression, the final value, is reached (or exceeded). eg Consider the following line:

For x = 6 To 26 Step 4

This loop generates a set of values {­6, 10, 14, 18, 22, 26} for the variable x.

CounterName takes the first of its set of values and the enclosed statements execute.

The Next statement prompts CounterName to take the next value from the defined range and the enclosed statements execute again. If it has taken all of its values, then the loop terminates and control passes to the line following the Next statement. Note that if StartExpression >= EndExpression, the enclosed statements are not executed.

If the Continue For statement is encountered, control returns to the line following the For statement after changing the value of CounterName. If the Exit For statement is encountered then the loop terminates and control passes to the line following the Next statement.

You could use the following Block statement structure, in place of the For structure.

block outer
CounterName = StartExpression

Goto test
block loop
statements
end block loop

CounterName = CounterName + IncrementExpression

block test
if CounterName > EndExpression then exit block outer
end block test

goto loop
end block outer

Note that EndExpression and IncrementExpression are evaluated each time they are encountered, unlike in some languages where they are evaluated once at the start. IncrementExpression may be negative and if so the > becomes < in block test. The correct (but complicated) test is:

if (IncrementExpression < 0 And CounterName < IncrementExpression) Or (IncrementExpression >= 0 And CounterName > IncrementExpression) then
exit block outer
end if

Statistically, For loops contain the most application programming errors. To help with this problem Equinox provides a number of statements (such as the Block structure) which help to simplify loop handling.

CategoryFlow Control
See Also Block_EndBlock, BreakPressed, Continue, Execute, EnableInput, Exit, For...Next, Goto, If_Then, Repeat_Until, Return, Sleep, Subtable, Switch, While_EndWhile
Example

The first example examines each character in the string txt. If a full stop, comma or hyphen is found, it is removed (replaced with nothing).

number i(4, 0)

for i = 1 to Len(txt)
if txt[i] >> ".-," then
txt[i] = ""
i = i - 1
end if
next

The next example places the string "10203040" into txt.

number i

txt=""

for i = 10 to 40 step 10
txt = txt & i
next

This final example inserts an oblique ("/") into txt at every third character, starting from the end. Hence, "abcd123" becomes "a/bcd/123".

for i = Len(txt) - 2 to 2 Step -3
Mid(txt, i, 0) = "/"
next i