FOR Statement
The FOR statement executes a limited number of iterations, using an integer index variable.
FOR Statement | |
---|---|
Name | FOR ... TO ... BY ... DO ... END_FOR |
Syntax | FOR <index> : = <mini> TO <maxi> BY <step> DO <statement> ; <statement> ; END_FOR; |
Operands | index: internal integer variable increased at each loopmini: initial value for index (before first loop)maxi: maximum allowed value for indexstep: index increment at each loop |
The [ BY step ] statement is optional. If not specified, the increment step is 1.
IMPORTANT:
Because the virtual machine is a synchronous
system, input variables are not refreshed during FOR iterations.
This is the "WHILE" equivalent of a FOR statement:
index := mini; while (index <= maxi) do <statement> ; <statement> ; index := index + step; end_while;
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;
To insert a FOR statement
- From theToolbox, drag theFORelement into the language container.
Provide Feedback