FOR loops in Structured Text: repeat work within a scan
The Core Idea
A FOR loop repeats a block of statements over a chosen index range during one execution of the program. It can apply the same calculation to each element of an array within the current scan.
What you will learn
Use a
FORloop to repeat the same work for every element of an array.Start a total, a count, or a minimum fresh before the loop, on every scan.
Find an extreme value by starting from a real element, not from a guess.
Why it matters
Collection calculations need to cover the intended elements and begin with the right working values. A loop makes that work compact but still requires clear boundaries.
The problem
The six-head filler from the arrays lesson reports each cycle's average fill and its lightest head. The weigh cells write all six readings into FILL_G at once; the checks feed in new cycles.
| Signal | Type | Meaning |
FILL_G | ARRAY[1..6] OF REAL | This cycle's fill weight of each head, in g. |
I | INT | The loop counter. |
TOTAL_G, AVG_G | REAL | This cycle's total and average fill, in g. |
LIGHTEST_G, LIGHTEST_HEAD | REAL, INT | This cycle's lightest fill, and which head it came from. |
FOR I := 1 TO 6 DO ... END_FOR; runs its body six times, with I set to 1, then 2, up to 6, all within one scan. Inside, FILL_G[I] is each head in turn. The loop adds every weight into TOTAL_G, and keeps the lightest one it has seen in LIGHTEST_G.
This cycle's readings are 498, 502, 504, 495, 510, and 497 g: a total of 3006 g, an average of 501.0 g, and head 4 is the lightest. The simulator scans every 100 ms. After you lock in your prediction, watch the human-machine interface (HMI) for a few seconds.
See it go wrong
Pause and press Step one scan a few times. Watch TOTAL_G in Tags grow by 3006 each scan.
Fix it
Change the program so the average stays at this cycle's value and the lightest is a real head. The checks feed in new cycles, including a heavier one. Editing pauses the program, so press Run to try your version.
Passing the checks records Structured Text progress on FOR loops.
What happened and why
A FOR loop finishes all its passes within one scan. Then the rest of the program runs, and on the next scan the whole program, loop included, runs again. Anything the loop builds up, such as a total, a count, or a lightest value, must therefore start fresh before the loop, every scan:
| Result | Start it at | Why |
| A total or a count | 0 | Adding starts from nothing. |
| A minimum or a maximum | The first element | Any real reading is a fair starting point; 0.0 is lighter than every head and wins every comparison. |
Starting it from 0.0 hides every reading. Starting it from the last scan's lightest keeps the lightest weight ever seen: after a cycle with 495 g, a cycle of all heavier heads still reports 495 g.
The counter I is an ordinary INT tag. The loop sets it to 1 at the start, and adds 1 after each pass. The last pass uses 6; after the loop, I is 7, so read the array inside the loop, not after it.
Common mistakes
Carrying the total from the last scan: the total and the average climb every scan. Set the total to 0.0 before the loop.
Starting the lightest at 0.0: no reading is lighter, so the lightest stays 0.0 g and the head never changes. Start from a real element.
Starting the lightest from the last cycle: a heavier cycle still reports the old lightest weight.
Looping past the end of the array:
FOR I := 1 TO 7readsFILL_G[7], which does not exist, and the program stops. Make the loop limits match the array's bounds.
Platform notes
Every pass of a loop adds to the scan time. Controllers stop a scan that runs too long with a watchdog fault, so keep loops short and bounded. This simulator stops a scan after 10 000 statements.
IEC 61131-3 (the international standard for programmable controller languages) lets a loop step by other amounts with
BY, for exampleFOR I := 6 TO 1 BY -1 DO. This simulator supportsBYtoo.
Enable JavaScript to run the embedded simulator and complete the interactive exercises.