The FOR/ENDFOR instruction is a function that repeats a loop within FOR and ENDFOR instructions a specified number of times.
FOR/ENDFOR instructions have two INSTRUCTIONs: FOR instruction and ENDFOR instruction.
FOR instruction - start of FOR/ENDFOR loop
ENDFOR instruction - end of FOR/ENDFOR loop
INSTRUCTIONs within FOR and ENDFOR instructions are repeated. The number of times to repeat is determined by the specified values in the FOR instruction.
The form of FOR instruction is as follows.
Register is used for the Loop counter. Constant, Register, and Argument Register is used for the Initial value. For Constant, an integer whose range is –32767 to 32767 can be specified. Constant, Register, and Argument Register is used for the Target value. For Constant, the integer whose range is –32767 to 32767 can be specified.
When the FOR instruction is executed, the Initial value is substituted into the Loop counter. The following condition should be satisfied to execute the FOR/ENDFOR loop.
When TO is specified, Initial value is equal to or smaller than Target value.
When DOWNTO is specified, Initial value is equal to or larger than Target value.
When this condition is not satisfied, the cursor moves to the next line of the combined ENDFOR instruction and the FOR/ENDFOR loop is not executed. The FOR instruction is executed only once in the FOR/ENDFOR loop.
The FOR/ENDFOR loop is repeated as long as the following condition is satisfied.
When TO is specified, the value of Loop counter is smaller than Target value.
When DOWNTO is specified, the value of Loop counter is larger than Target value.
When this condition is satisfied and TO is specified, the value of Loop counter is incremented. When this condition is satisfied and DOWNTO is specified, the value of the Loop counter is decremented. And after that, the cursor moves to the next line of the combined FOR instruction. When this condition is not satisfied, the cursor moves to next line and the FOR/ENDFOR loop is not repeated.
An internal delay is used when the FOR/ENDFOR loop is executed repeatedly. Therefore, a WAIT instruction is not necessary in the FOR/ENDFOR loop to repeat the loop.
How to teach FOR/ENDFOR instruction is as follows.
Procedure 7. Teach FOR/ENDFOR Instruction
In this example, insert blank lines in line 1 and 5.
Move the cursor to line 1 and press F1, [INST].
Select FOR/ENDFOR.
Select FOR. The FOR instruction is taught.
The cursor automatically moves to the index of the register. Input the value of the index. In this example, input 1.
The cursor automatically moves to Initial value part. In this example, Constant is selected and the value is 1. To choose register or argument register, press F4, [CHOICE] and select one of them.
The cursor automatically moves to TO/DOWNTO part. In this example, press ENTER. To choose DOWNTO, press F4, [CHOICE] and select DOWNTO.
The cursor automatically moves to Target value part. In this example, select R[]. Input 2 for the index of the register.
Move the cursor to line 5 and press F1, [INST].
Select FOR/ENDFOR.
Select ENDFOR. ENDFOR instruction is taught.
FOR and ENDFOR instructions are automatically combined after teaching. The closest FOR instruction and ENDFOR instruction are combined. By teaching FOR/ENDFOR instructions additionally in FOR/ENDFOR loop, nested loops can be formed. Up to 10 nested loops can be formed. However, teaching more than 10 nested loops causes an alarm in execution.
The number of FOR instruction and ENDFOR instruction in a program should be the same. When the number is not the same, an alarm occurs in execution. Alarms related to FOR/ENDFOR function are described in Section 9.5, "Alarms" . How to combine FOR and ENDFOR instructions is described in the following example. Teach FOR instruction in line 1. In this case, the number of FOR instruction and ENDFOR instruction is not the same. Therefore, if this program is executed, the alarm “INTP-670 Need ENDFOR for FOR in line 1” occurs.
Teach ENDFOR instruction in line 6. FOR/ENDFOR loop is formed by FOR instruction in line 1 and ENDFOR instruction in line 6.
Teach FOR instruction in line 3. In this case, the number of FOR instruction and ENDFOR instruction is not the same. Therefore, if this program is executed, the alarm “INTP-670 Need ENDFOR for FOR in line 1” occurs.
Teach ENDFOR instruction in line 8. The closest FOR instruction and ENDFOR instructions are combined. Therefore, the first FOR/ENDFOR loop is formed by FOR instruction in line 3 and ENDFOR instruction in line 6, and the second FOR/ENDFOR loop is formed by FOR instruction in line 1 and ENDFOR instruction in line 8. In this example, 2 nested loops are formed.
Please be careful not to use the same register number as Loop counters in the same nested loop. It might cause abnormal behavior.
Backward execution is prohibited on FOR/ENDFOR instructions. However, backward execution on instructions within FOR/ENDFOR loop is allowed. In the following example, when backward execution is started from line 1 or 5, the alarm “INTP-238 BWD execution completed” occurs. On the other hand, backward execution can be done when started from line 2, 3, 4 or 6.
The examples of FOR/ENDFOR instruction are described with the following program.
Case 1: R[2]=3 TO is specified and Initial value is smaller than Target value. Therefore, the condition of FOR instruction is satisfied. As the value of Loop counter changes from 1 to 3, the condition of ENDFOR instruction is satisfied and FOR/ENDFOR loop is repeated three times.
Case 2: R[2]=1 TO is specified and Initial value is equal to Target value. Therefore, the condition of FOR instruction is satisfied. However, as the value of Loop counter is equal to Target value, the condition of ENDFOR instruction is not satisfied. Therefore, FOR/ENDFOR loop is executed only once.
Case 3: R[2]=0 TO is specified and Initial value is larger than Target value. Therefore, the condition of FOR instruction is not satisfied. The cursor moves to the next line of combined ENDFOR instruction, that is, line 6 and FOR/ENDFOR loop is not executed.
Other examples Start within FOR/ENDFOR loop: R[1]=0 When the following program is executed from line 3 and R[1]=0, the condition of ENDFOR instruction is satisfied. Therefore, FOR/ENDFOR loop is repeated 4 times (0 to 3).
Start within FOR /ENDFOR loop: R[1]=5 When the following program is executed from line 3 and R[1]=5, the condition of ENDFOR instruction is not satisfied. Therefore, FOR/ENDFOR loop is not repeated.
JMP/LBL instructions exist within FOR/ENDFOR loop: In the following program, JMP/LBL instructions exist within the FOR/ENDFOR loop. The cursor moves from line 3 to line 7 by JMP instruction. FOR/ENDFOR loop between line 1 and 4 is not executed. As line 7 is also within the FOR/ENDFOR loop and the condition of ENDFOR instruction in line 8 is satisfied, FOR/ENDFOR loop between lines 5 and 8 is repeated. Finally, the value of R[1] is equal to 1 and R[2] is equal to 2.
Please be careful when you use JMP/LBL within FOR/ENDFOR loop. Before you use them, please consider how the loop works. Otherwise, it might cause fatal errors.
The Loop counter value is modified within FOR/ENDFOR loop: Loop counter can be modified within the FOR/ENDFOR loops. In the following example, when DI[1] = ON, R[1] is changed to 11. As this value is larger than Target value, this FOR/ENDFOR loop is finished.
Please be careful when you modify Loop counter value within FOR/ENDFOR loop. Changing Loop counter value within FOR/ENDFOR loop may cause an abnormal behavior or infinite loops. Please be careful when you modify Target value within FOR/ENDFOR loop when register or argument register is specified for them. Changing the value within FOR/ENDFOR loop might cause an abnormal behavior.
The Loop counter value is calculated within FOR/ENDFOR loop: In the following example, FOR/ENDFOR loop counter is calculated. After execution, Loop counter value will be 11. Loop counter value is different from Target value. However, this result is correct.
This is explained as follows. Line 1, R[1] is 6 (Initial value). Line 5, R[1] is 7. Line 6, R[1] is 7 and this satisfies the ENDFOR condition. R[1] is incremented to be 8. Line 5, R[1] is 9. Line 6, R[1] is 9 and this satisfies the ENDFOR condition. R[1] is incremented to be 10. Line 5, R[1] is 11. Line 6, R[1] is 11 and this does not satisfy the ENDFOR condition. The cursor goes to next line. This means the user can calculate Loop counter, but need to be careful that the Loop counter value is different from Target value.
In some cases, calculating Loop counter causes an alarm. This problem can be avoided by using DIV just before FOR/ENDFOR instruction or input the same value in DATA screen. Especially when you calculate with real value use division, please make sure to use DIV. R[1]=R[1] DIV 1
Alarms occur in the following conditions with FOR/ENDFOR function.
Execute when the number of FOR instruction is smaller than ENDFOR instruction
Execute when the number of ENDFOR instruction is smaller than FOR instruction
Execute when there is over 10 nested loops
Other than integer is used for Initial value or Target value in FOR instruction
Other than integer is used for the value of Loop counter or Target value in ENDFOR instruction
Execute when the number of FOR instruction is smaller than ENDFOR instruction
Execute the following program. As FOR instruction for ENDFOR instruction in line 5 does not exist, the alarm “INTP-669 Need FOR for ENDFOR in line 5” occurs in execution.
Execute when the number of ENDFOR instruction is smaller than FOR instruction Execute the following program. As ENDFOR instruction for FOR instruction in line 1 does not exist, the alarm “INTP-670 Need ENDFOR for FOR in line 1” occurs in execution.
Execute when there are over 10 nested loops The alarm “INTP-671 Too many FOR” occurs in execution.
Other than integer is used for Initial value or Target value in FOR/ENDFOR instruction
Other than integer is used for the value of Loop counter or Target value in ENDFOR instruction Execute the following program. As the value of the register for Target value in FOR instruction in line 2 is not integer, the alarm “INTP-672 (program, 2) Value type is not integer” occurs when line 2 is executed. In other cases, calculating the Loop counter, Initial value or Target value may cause the same alarm.
In some cases calculating a register used for Loop counter or Target value causes the alarm above even though the result and the value displayed in DATA screen is integer. This problem can be avoided by using DIV as follows just before FOR/ENDFOR instruction or input the same value in DATA screen. Especially when you calculate with real value using division, please make sure to use DIV. R[1]=R[1] DIV 1