FOR_DO

Use the FOR_DO loop to perform an action a number of times before doing anything else.
When enabled, the FOR instruction repeatedly executes the Routine until the Index value exceeds the Terminal value. 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.
Do not loop too many times in a single scan. An excessive number of repetitions causes the controller watchdog to timeout and causes a major fault.
Operands
FOR count:= initial_value TO
final_value BY increment DO
<statement>;
END_FOR;
Operand
Type
Format
Description
count
SINT INT DINT
Tag
Tag to store count position as the FOR_DO executes
initial_ value
SINT INT DINT
Tag expression Immediate
Must evaluate to a number
Specifies initial value for count
final_ value
SINT INT DINT
Tag expression Immediate
Specifies final value for count, which determines when to exit the loop
increment
SINT INT DINT
Tag expression Immediate
(Optional) amount to increment count each time through the loop
If you don’t specify an increment, the count increments by 1.
IMPORTANT:
Do not iterate within the loop too many times in a single scan.
The controller does not execute other statements in the routine until it completes the loop.
A major fault occurs when completing the loop takes longer than the watchdog timer for the task.
Consider using a different construct, such as IF_THEN.
Description
The syntax is described in the table.
FOR_DO_syntax
This diagrams illustrates how a FOR_DO loop executes, and how an EXIT statement leaves the loop early.
FOR_DO Executes
FOR DO exits
The FOR_DO loop executes a specific number of times.
To stop the loop before the count reaches the last value, use an EXIT statement.
Affects Math Status Flags
No
Major/Minor Faults
A major fault will occur if
Fault type
Fault code
The construct loops too long.
6
1
Example 1
If performing the following,
Enter this structured text
Clear bits 0…31 in an array of BOOLs:
Initialize the subscript tag to 0.
Clear i . For example, when subscript = 5, clear array[5].
Add 1 to subscript.
If subscript is ≤ to 31, repeat 2 and 3.
Otherwise, stop.
For subscript:=0 to 31 by 1 do
array[subscript] := 0;
End_for;
Example 2
If performing the following,
Enter this structured text
A user-defined data type (structure) stores the following information about an item in your inventory:
  • Barcode ID of the item (String data type)
  • Quantity in stock of the item (DINT data type)
An array of the above structure contains an element for each different item in your inventory. You want to search the array for a specific product (use its bar code) and determine the quantity that is in stock.
  1. Get the size (number of items) of the Inventory array and store the result in
  2. Inventory_Items (DINT tag).
Initialize the position tag to 0.
  1. If Barcode matches the ID of an item in the array, then:
Set the Quantity tag = Inventory[position].Qty. This produces the quantity in stock of the item.
Stop.
Barcode is a string tag that stores the bar code of the item for which you are searching. For example, when
position = 5, compare Barcode to Inventory[5].ID.
  1. Add 1 to position.
  2. If position is ≤ to (Inventory_Items -1), repeat 3 and 4. Since element numbers start at 0, the last element is 1 less than the number of elements in the array.
Otherwise, stop.
SIZE(Inventory,0,Inventory_Items);
For position:=0 to Inventory_Items - 1 do
If Barcode = Inventory[position].ID then
Quantity := Inventory[position].Qty;
Exit;
End_if;
End_for;
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.