Lorne your understanding of the for..next loop is essentially correct.
TRI's for..next documentation is not correct. Their text is in green. I've struck out the things that are flat out wrong and have added text in red for some of the stuff that they omited:
FOR variable = x TO y [STEP z]
. . . .
NEXT
where variable may be any integer variable only including A to Z, DM[] and DM32 and is used as a counter. x, y and z are numeric expressions either a constant value such as 1 or a variable such as J. Other numeric expressions are NOT allowed. STEP z is an optional part of the statement. x is the initial value of the counter, y is the final value of the counter. Program lines following the FOR statement are executed until the NEXT statement is encountered. Then the counter is incremented by the amount specified by STEP. If STEP is not specified, the increment is assumed to be 1. A check is performed to see if the value of the counter is greater than the final value y if STEP is positive (or smaller than the y if STEP is negative). If it is not greater, the program branches back to the statement after the FOR statement, and the process is repeated. If it is greater, execution continues with the statement following the NEXT statement. This is called a FOR-NEXT loop. A run-time error will result if STEP is evaluated to be 0.
For PLCs that support floating point, the x, y and z values can be floating point constants or simple floating point variables such as A#..Z# and FP[1..1000].
If the x,y or z values are located in DM[], DM32[] or FP[] the compiler will allow limited arithmetic expressions that involve constants and variables. The following sort of expressions will run under simulation:
DM[0+1] and DM[n+1].
Please note that the values of the control, start, end and start arguments are evaluated and stored when the "for" part of the for..next statement is executed. Additionally the control variable's address is stored. The end result of this is that if you specify a for..next loop using variables, changing these variables within the body of the for..next loop will have no effect on the operation of the loop.
The following PLC code will work:
' integer variable, constant start and end values
'
print #3 "for i = 1 to 10: ";
for i = 1 to 10
print #3 i;" ";
next
print #3 ""
' integer variable DM[1], constant start and end values
'
print #3 "for dm[1] = 1 to 10: ";
for dm[1] = 1 to 10
print #3 dm[1];" ";
next
print #3 ""
' integer variable, constant start, variable end
'
k = 15
print #3 "for i = 10 to k: ";
for i = 10 to k
print #3 i;" ";
next
print #3 ""
' integer variable, variables start and end values
'
j = 10
k = 15
print #3 "for i = j to k: ";
for i = j to k
print #3 i;" ";
next
print #3 ""
' integer start, end and step values
'
print #3 "for i = 10 to 1 step -1: ";
for i = 10 to 1 step -1
print #3 i;" ";
next
print #3 ""
' integer start and end, variable step value
'
j = -2
print #3 "for i = 10 to 0 step j: ";
for i = 10 to 0 step j
print #3 i;" ";
next
print #3 ""
' float variable, constant start and end values
'
print #3 "for i# = 1.0 to 10.0: ";
for i# = 1.0 to 10.0
print #3 i#;" ";
next
print #3 ""
' float variable, variable start and end values
'
j# = 10.0
k# = 15.0
print #3 "for i# = j# to k#: ";
for i# = j# to k#
print #3 i#;" ";
next
print #3 ""
[/font]
When run under the simulator the code will write this to a window:
for i = 1 to 10: 1 2 3 4 5 6 7 8 9 10
for dm[1] = 1 to 10: 1 2 3 4 5 6 7 8 9 10
for i = 10 to k: 10 11 12 13 14 15
for i = j to k: 10 11 12 13 14 15
for i = 10 to 1 step -1: 10 9 8 7 6 5 4 3 2 1
for i = 10 to 0 step j: 10 8 6 4 2 0
for i# = 1.0 to 10.0: 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
for i# = j# to k#: 10.0 11.0 12.0 13.0 14.0 15.0
Lorne there are a lot of things that you can do with the for..next structure.
Once you get this mastered, that TBASIC also supports another powerful looping structure: while..endwhile. The other very useful control structure is if..elif..elif..else..endif.
No end of stuff to learn.
Gary D