- Name:
-
next
- Syntax:
-
FOR variable = start TO end
{code}
NEXT {variable}
FOR variable = start TO end STEP {-}increment
{code}
NEXT {variable}
Variable - will be used as the loop counter
Start - is the initial value of variable
End - is the finish value of variable
Increment - is an optional value which overrides the default counter value of +1. If Increment is preceded by a '-', it will be assumed that Start is greater than End, and therefore increment will be subtracted (rather than added) on each loop.
- Description:
-
The 'next' command indicates the end of the 'for...next' loop and tests the control variable's value to see if the code within the 'for...next' loop should be repeated or whether execution should continue after the 'next' command.
The control variable name may optionally be placed after the 'next' command and if used must match the control variable named in the 'for' command.
A 'for...next' command will always be executed at least once, regardless of start and end values. Whether to repeat the code within the 'for...next' loop is determined upon execution of the 'next' command, not upon execution of the 'for' command as may be the case in some other programming languages.
The 'for...next' start and end values are evaluated every time the 'next command is executed so if the start or end values are specified by variables, altering those variables within the 'for...next' loop can affect how many times the loop will execute. This may cause the 'for...next' to repeat indefinitely or more times than expected.