FOR
Statement
Executes the provided statements while incrementing or decrementing a counter
variable at each iteration.The loop may be terminated without regard to the counter by using the
BREAK
statement. The current iteration of the loop can be abandoned and the next
iteration of the loop started by using the CONTINUE
statement.
Syntax
Parameters
- variable-name: variable name
The name of the counter variable. The variable name must begin with an at sign (@
), dollar sign ($
), or colon (:
). The variable need not be previously declared; theFOR
statement acts as a variable declaration if needed. This variable will receive an integer value that changes with each iteration of the loop. - first-number: integer
The value of the counter variable on the first iteration of the loop. - last-number: integer
The loop stops if the counter steps past this value. - step: integer (optional)
The number to add to the counter variable on each iteration. If first-number ≤ last-number then the default step is 1, otherwise the default step is -1. - statement: statement
The statements to execute on each iteration of the loop. If more than one statement is desired, theBEGIN
andEND
keywords must be used.
Example
-- Prints the numbers: 1, 2, 3, 4, 5.
FOR @i = 1 TO 5
PRINT @i;
-- Prints the numbers: 5, 4, 3, 2, 1.
FOR @i = 5 TO 1
PRINT @i;
-- Prints the numbers: 1, 3, 5, 9.
FOR @i = 1 TO 10 STEP 2
PRINT @i;