11 - 70 11 - 70
MELSEC-Q
11 INSTRUCTIONS AND FUNCTIONS
FOR to NEXT
Instruction
FOR to NEXT
• Executes a series of instructions for the specified number of times.
FOR <variable name> = <initial value> TO <final value> [STEP <increment> ]
NEXT [ <variable name> ] [, <variable name> ]
variable name
• • • •
Specify a variable that controls the repeat.
initial value
• • • •
Specify the initial value of the variable that controls
the repeat.
final value
• • • •
Specify the final value of the variable that controls
the repeat.
Syntax
increment
• • • •
Specify the increment of the variable that controls
the repeat.
FOR I=0 TO 100 STEP 2
to
• • • •
Examples
NEXT I
Repeatedly executes through NEXT I by changing
variable I from 0 to 100 with increment 2.
Description
• The FOR to NEXT instruction repeatedly executes the instructions between FOR and
NEXT for the specified number of times.
• Only an integer variable and single precision variable can be used for the variable specified
by <variable name>. Character variable and double precision variable cannot be used.
• When instructions between the FOR instruction and the NEXT instruction are executed, an
increment specified by STEP is added to the variable and compared to the final value.
If the value of the variable is equal to or less than the final value, the same processing is
repeated after returning to the instruction following the FOR instruction. If the value of the
variable is larger than the final value, the instruction after the NEXT instruction will be
executed.
• The increment of the variable is considered 1 if STEP is omitted.
If the value specified by STEP is negative, the final value must be smaller than the initial
value. In this case, the variable decrements after every repeat and it continues until the
value of the variable is less than the final value.
If the STEP value is positive and the initial value is larger than the final value, or if the
STEP value is negative and the initial value is smaller than the final value, the instructions
between the FOR instruction and the NEXT instruction is executed only once.
• The FOR to NEXT loop can be nested. A FOR to NEXT loop can be inside of another FOR
to NEXT loop.
If the loop is nested, the variable used in each loop has to be distinct. A FOR to NEXT
instruction loop has to be within another FOR to NEXT instruction.
FOR I=0 TO 10
FOR J=0 TO 10
to Repeat Repeat
NEXT J
NEXT I