FOR, NEXT Program Control

Inputs/Outputs Operands Data Types
INDX VW, IW, QW, MW, SW, SMW, LW, T, C, AC, *VD, *AC, *LD INT
INIT VW, IW, QW, MW, SW, SMW, T, C, AC, LW, AIW, Constant, *VD, *AC, *LD INT
FINAL VW, IW, QW, MW, SW, SMW, LW, T, C, AC, AIW, Constant, *VD, *AC, *LD INT

Memory Ranges ENO Errors Instruction Support for S7-200 CPUs SIMATIC/International Mnemonics
The FOR instruction executes the instructions between the FOR and the NEXT. You must specify the index value or current loop count (INDX), the starting value (INIT), and the ending value (FINAL).

The NEXT instruction marks the end of the FOR loop, and sets the top of the stack to 1.

For example, given an INIT value of 1 and a FINAL value of 10, the instructions between the FOR and the NEXT are executed 10 times with the INDX value being incremented: 1, 2, 3, ...10.

If the starting value is greater than the final value, the loop is not executed. After each execution of the instructions between the FOR and the NEXT instruction, the INDX value is incremented and the result is compared to the final value. If the INDX is greater than the final value, the loop is terminated.

FOR: Error Conditions that Set ENO = 0:

SM4.3 (run-time), 0006 (indirect address)

Here are some guidelines for using the FOR/NEXT loop:

·If you enable the FOR/NEXT loop, it continues the looping process until it finishes the iterations, unless you change the final value from within the loop itself. You can change the values while the FOR/NEXT is in the looping process.

·When the loop is enabled again, it copies the initial value into the index value (current loop number). The FOR/NEXT instruction resets itself the next time it is enabled.

Use the FOR/NEXT instructions to delineate a loop that is repeated for the specified count. Each FOR instruction requires a NEXT instruction. You can nest FOR/NEXT loops (place a FOR/NEXT loop within a FOR/NEXT loop) to a depth of eight.