for Statement
A
for loop lets you run a section of code a specific number of times.
The loop variable counts up or down with each execution of the loop.
The loop body is repeatedly executed until the loop variable reaches the end number.
Syntax
for <VARIABLE> = <START> to <END> [step <AMOUNT>]
<BODY>
next
- <VARIABLE> is the name of a new loop variable to count up or down.
- <START> is the first number to start with.
- <END> is the last number to end with. The loop exits after the loop variable passes this number.
- <AMOUNT> is the amount to add to the loop variable each time the loop body is executed. If not specified, the default is 1. It can be negative to count down.
Usage
The following example loops 10 times and prints the loop variable at each iteration.
for i = 1 to 10
print i
next
The
exit for statement can be used to exit a loop before reaching the end number.
This example prints the numbers 1 to 4, because the
exit for statement exits the loop when the loop variable
i is 5.
for i = 1 to 10
if i = 5 then
exit for
end if
print i
next