LOOP FOR
Syntax
![]()
Command
Mandatory parameter Optional parameter Mandatory keyword Optional keyword |
LOOP FOR <Var> := <Startvalue> TO | DOWNTO <Endvalue> [STEP <Step value>]
<Code lines>
ENDLOOP
Description
Repeats execution of the enclosed code until the variable in the FOR-condition is greater than the endvalue (or: less than the endvalue in case of a DOWNTO loop).
Diagram
Returned value type
None
Parameters
Parameter | Description |
<Var> |
The name of the count variable to be used for holding the current count value. |
<Startvalue> |
Initial value for the count variable when the loop is executed for the first time. |
<Endvalue> |
Value in the condition that determines whether to continue or stop the execution of the loop. The execution will stop when the count value is greater than the endvalue in case of a TO loop and when the count value is less than the endvalue in case of a DOWNTO loop. |
<Step value> |
Value that is added to the current count value at the end of the execution of each loop. Note: In case of a DOWNTO the STEP value must be negative. |
<Code lines> |
Code that is performed each time the loop is executed. The loop will be terminated immediately in one of the following cases:
|
TO |
The count variable will be incremented at the end of each loop. If the value is greater than the endvalue the execution of the loop will be terminated. |
DOWNTO |
The count variable will be decremented at the end of each loop. If the value is lesser than the endvalue the execution of the loop will be terminated. |
STEP |
The
count variable will be incremented by the value set in <Step
value>. The default value is 1 when the TO keyword is used and -1
when the DOWNTO keyword is used. Make sure that when using the DOWNTO
keyword and defining a non default step value, the value is negative. |
Remarks
The evaluation of the condition is performed at the start of each loop and the increment/decrement of the count variable is at the end of each loop. So a 'LOOP FOR i := 10 TO 10' will execute once and a 'LOOP FOR i := 10 TO 9' will execute never.
As in Visual Basic, when using DOWNTO and STEP, STEP should be a negative number.
Examples
loop for i := 1 to 5
sme ('I is: ' + format (i, onezero))
endloop
loop for i := 0 to 5 step 2
sme ('I is: ' + format (i, onezero))
endloop
loop for i := 5 downto 1
sme ('I is: ' + format (i, onezero))
endloop
sme ('I is: ' + format (i, onezero))
endloop
loop for i := 0 to 5 step 2
sme ('I is: ' + format (i, onezero))
endloop
loop for i := 5 downto 1
sme ('I is: ' + format (i, onezero))
endloop