Implementing a FOR loop
Since the FOR loop is a counter loop, it needs three things to function. The needed components are as follows:
- A starting value (typically 0, but it can be other values)
- A termination condition
- Incrementing/decrementing logic
In the IEC 61131-3 standard, there is a fourth, optional component that can be used to determine the step. In other words, it controls how much the loop will increase or decrease by. By default, the FOR loop will increase by 1. The general syntax for the FOR loop is as follows:
FOR <variable> := start_value to end_value <BY step> DO Code END_FOR;
In this example, the BY step is the optional code that will increase or decrease the FOR loop by a stipulated value.
As with every other concept explored in this book, let’s implement the loop to see how it behaves. For this example, let’s create a loop that will count from 0 to 100. To do this, we will need...