For (FOR)
The FOR instruction executes a routine repeatedly.
When enabled, the FOR instruction repeatedly executes the Routine until the Index value exceeds the Terminal value. This instruction does not pass parameters to the routine.
The step value can be positive or negative. If it is negative, the loop ends when the index is less than the terminal value. If it is positive, the loop ends when the index is greater than the terminal value.
Each time the FOR instruction executes the routine, it adds the Step size to the Index.
Be careful not to loop too many times in a single scan. An excessive number of repetitions can cause the controller’s watchdog to timeout, which causes a major fault.
Available Languages
Ladder Diagram
Operands
Ladder Diagram
Operand | Type | Format | Description |
---|---|---|---|
Routine name | ROUTINE | tag | Subroutine that is invoked each time the FOR loop executes. |
Index | DINT | tag | Counts how many times the routine has been executed |
Initial value | SINT INT DINT | immediate tag | Value at which to start the index |
Terminal value | SINT INT DINT | immediate tag | Value at which to stop executing the routine |
Step size | SINT INT DINT | immediate tag | Amount to add to the index each time the FOR instruction executes the routine |
Affects Math Status Flags
No
Major/Minor Faults
A major fault will occur if: | Fault type | Fault code |
---|---|---|
The nesting level limit > 25 | 4 | 94 |
the subroutine is an SFC and it is already executing (recursive call) | 4 | 82 |
See Common Attributes for operand-related faults.
Execution
Condition/State | Action |
---|---|
Prescan | The instruction will prescan the named subroutine if it has never been prescanned before. Tip: If recursive FOR instruction exist to the same subroutine, or multiple FOR instruction exist (non-recursive) to the same subroutine, the subroutine is pre-scanned only once. This is also true if the subordinate was prescanned by a JSR. |
Rung-condition-in is false | N/A |
Rung-condition-in is true | See the following FOR Flow Chart (True). |
Postscan | The instruction will postscan the named subroutine exactly once. |
FOR Flow Chart (True)
Examples
When enabled, the FOR instruction repeatedly executes routine_2 and increments value_2 by 1 each time. When value_2 is > 50000 or a BRK instruction is enabled, the FOR instruction no longer executes routine_2.
Provide Feedback