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.
Properties of FOR statement
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
FOR <index> := <mini> TO <maxi> BY <step> DO <statement> ; <statement> ; END_FOR;
Operands
  • Index: Internal integer variable increased at each loop
  • Mini: Initial value for index before first loop
  • Maxi: Maximum allowed value for index
  • Step: Index increment at each loop

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
Have questions or feedback about this documentation? Please submit your feedback here.
Normal