Use program loops
- Last UpdatedJul 23, 2024
- 2 minute read
Loops allow you to execute a section of code repeatedly. The InTouch HMI only supports FOR loops. A FOR loop works by monitoring the value of a numeric loop variable that is incremented or decremented with each loop iteration. The loop is executed until the value of the loop variable reaches a fixed limit.
Syntax
FOR LoopTag = StartExpression TO EndExpression [STEP ChangeExpression]
... statements or another FOR loop ...
NEXT;
-
StartExpression, EndExpression and ChangeExpression together define the number of iterations.
-
StartExpression sets the start value of the loop range. EndExpression sets the end value of the loop range.
-
STEP ChangeExpression optionally sets the value by which the loop tag is incremented or decremented during each loop iteration; if you do not specify this, a default of 1 is used.
When you execute a FOR loop, the InTouch HMI:
-
Sets LoopTag to the value of StartExpression.
-
Tests whether LoopTag is greater than EndExpression. If so, the InTouch HMI exits the loop. (If ChangeExpression is negative, the InTouch HMI tests whether LoopTag is less than EndExpression.)
-
Executes the statements within the loop.
-
Increments LoopTag by the value of ChangeExpression (1 unless otherwise specified).
-
Repeats steps 2 through 4.
Remember the following rules when working with FOR loops:
-
FOR loops can be nested. The maximum number of nesting levels depends on the available memory and system resources.
-
For every FOR statement, there must be a closing NEXT statement. A NEXT statement always applies to the nearest prior FOR statement on the same nesting level.
-
LoopTag must be a numeric tag (or local variable).
-
StartExpression, EndExpression and ChangeExpression must be valid expressions that evaluate to a numeric result.
-
If ChangeExpression is positive, EndExpression must be greater than StartExpression; if ChangeExpression is negative, StartExpression must be greater than EndExpression. Otherwise, the loop does not start.
-
To exit a loop, use the EXIT FOR statement. For more information, see Force the end of a loop.
-
There is a time limit for loops. See Time limit for loop execution.
Caution: Loop execution affects other run-time processes. For more information, see Effect of loops on other run-time processes.