2 Instruction Descriptions
2-78
NJ-series Instructions Reference Manual (W502)
• FOR-NEXT loops (or FOR-END_FOR loops in ST) can be nested. In the following figure, the pro-
cesses are performed in the following order.
Process A → Process B → Process B → Process C → Process A → Process B → Process B → Pro-
cess C → Process A → Process B → Process B → Process C
• In a ladder diagram, connect the FOR and NEXT instructions directly to the left bus bar.
• If you use this instruction in ST, you can use a function or expression that returns an integer for
InitVal. You cannot use a function or expression for EndVal or StepVal.
• Always use the FOR and NEXT instructions (FOR and END_FOR statements in ST) as a pair. A pro-
gramming error will occur if there is not the same number of both instructions.
• Always use the FOR-NEXT pair (the FOR-END_FOR pair in ST) in the same program section.
• If the value of InitVal is less than the value of EndVal, use a positive number for the value of StepVal.
If the value of InitVal is greater than the value of EndVal, use a negative number for the value of
StepVal.
• Set the condition to end repetition carefully so that you do not create an infinite loop.
Example: If the values that are given in the following table are used for the input parameters to
the variables, the value of Index will never be greater than the value of EndVal because
the maximum value of SINT data is 255. Therefore, an infinite loop is created.
• The FOR-NEXT loops can be nested up to 15 levels, but count all nesting levels for the following
instructions: IF, CASE, FOR, WHILE, and REPEAT.
• If loops are nested, you will need one BREAK instruction (or one EXIT instruction in ST) for each
nesting level to cancel all repeat processing.
• Do not use Jump Instructions (e.g., the JMP instruction) to interrupt repeat processing. Always use a
BREAK instruction (or an EXIT instruction in ST) to cancel repeat processing.
• You can change the values of StepVal and EndVal during repeat processing. You cannot change the
value of InitVal during repeat processing.
• If the value of StepVal is 0, a task execution timeout occurs.
• Use the same data type for InitVal, EndVal, StepVal, and Index. Otherwise, a building error will occur.
Precautions for Correct Use
Variable Value of input parameter
InitVal SINT#0
EndVal SINT#255
StepVal SINT#1
Index ---
Process
A
Process
B
Process
C
NEXT
NEXT
FOR
FOR
InitVal=INT#0
EndVal=INT#2
StepVal=INT#1
InitVal=INT#0
EndVal=INT#1
StepVal=INT#1