Basic programming guide – Remote Processing BASIC for the CX-10 Modbus User Manual
Page 28

BASIC PROGRAMMING GUIDE
2-19
FOR-TO-STEP-NEXT
Syntax:
FOR variable=initial index expr TO index limit expr [STEP step expr ]
program statements
NEXT [variable]
Where: variable = any valid variable symbol
initial index expr = starting value assigned to variable
index limit expr = ending value of variable
step expr = optional increment or decrement to variable when repeating a loop
Function:
Looping structure useful for executing a sequence of instructions a number of times.
Mode:
Run, command
Use:
FOR A=0 to 4000 STEP 200 : AOT 0,A : NEXT
DESCRIPTION
The FOR-TO-STEP-NEXT instruction is a loop structure common to many high level languages. It is used to
perform program statements a number of times.
variable is a loop counter initialized to initial index expr at the start of the loop. A number of program
statements are executed until NEXT is encountered. At this point the value of step expr is added to the value
of variable. The resulting new variable value is compared to the value of index limit expr. If the new value
of variable value is less than or equal to the value of index limit expr, all program statements are executed
again, and the test is repeated.
program statements are always executed at least once. If step expr is larger than index limit expr, the loop
executes only once.
STEP is optional. When omitted, it defaults to 1. The value of step expr may be positive or negative.
FOR-NEXT loops may be inside other FOR-NEXT loops. variable following NEXT is optional.
There are two ways to break out of a for next loop and still maintain the control stack. The first is to execute
a CLEAR S command. This command also clears any subroutine return locations and DO-WHILE, DO-
UNTIL loops. Another is to set variable to a high value within program statements. When a program
continuously breaks out of a FOR-NEXT loop and re-declares a new loop, a C-Stack error is eventually
returned.
RELATED
DO-UNTIL, DO-WHILE
ERROR
C-STACK NEXT without a corresponding FOR. This error can also appear if a number of FOR-NEXT
loops were set up but were illegally branched out of or re-declared.