Structured Text Programming
Rockwell Automation Publication 1756-RM006K-EN-P - November 2018 527
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.
3. 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.
4. Add 1 to position.
5. 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;
Use IF_THEN to complete an action when specific conditions occur.
Operands
IF bool_expression THEN
<statement>;
Operand Type Format Enter
Bool_
expression
BOOL Tag expression BOOL tag or expression that evaluates to a BOOL value
(BOOL expression)