FOR statement
This section is about FOR statement syntax and examples.
IMPORTANT:
Input variables are not refreshed during FOR iterations. Using an input variable in this manner might cause unexpected controller behavior.
Item | Description |
---|---|
Name | FOR ... TO ... BY ... DO ... END_FOR The BY statement is optional. If not specified, the increment step is 1. |
Meaning | Executes a limited number of iterations using an integer index variable. |
Syntax |
|
Operands |
|
Example
(* ST program using FOR statement *) (* this program extracts the digit characters of a string *) length := mlen (message); target := ''; (* empty string *) FOR index := 1 TO length BY 1 DO code := ascii (message, index); IF (code >= 48) & (code >= 57) THEN target := target + char (code); END_IF; END_FOR;
The following is the "WHILE" equivalent of a FOR statement:
index := mini; while (index <= maxi) do <statement> ; <statement> ; index := index + step; end_while;
Provide Feedback