Repeats a group of statements a specified number of times.
First the expression start and end are evaluated. When the NEXT statement is encountered, stepSize amount is added to the variable defined as the counter. At this point, if counter is less than or equal to end value, the statements in the loop execute again. If counter is greater than end, then the loop is exited and execution continues with the statement following the NEXT statement.
The expressions start, end and stepSize can be any expression or variable of any type. However, unlike with the WHILE statement, if end is an expression, the FOR statement evaluates this expression only once at the start of the loop and stores this value for subsequent comparisons. So you should not write code that relies on the end expression being evaluated every time through the loop.
Syntax: |
---|
FOR counter = start TO end [STEP stepSize ] |
Parameter: |
Description: |
---|---|
counter |
Variable used as a loop counter. This variable must be declared as an integer before using. |
start |
Initial value of counter (can be a complex expression) |
end |
Final value of counter (can be a complex expression) |
[STEP stepSize ] |
Integer amount counter is changed each time through the loop. If not specified, step defaults to one, and the step value will increment. Step value can be a negative that decrements to the value contained in the variable: end. |
statements |
One or more statements between FOR and NEXT that are executed the specified number of times. |
Example: |
---|
' ------------------------------------------------- |
Returns: |
Example 1: index value = 1 index value = 2 index value = 3 index value = 4 index value = 5
Example 2: index value = -1 index value = -2 index value = -3 index value = -4 index value = -5
Example 3: Month: Jan Day # = 1 Day Name = SUNDAY Day # = 2 Day Name = MONDAY Day # = 3 Day Name = TUESDAY Day # = 4 Day Name = WEDNESDAY Day # = 5 Day Name = THURSDAY Day # = 6 Day Name = FRIDAY Day # = 7 Day Name = SATURDAY
Month: Feb Day # = 1 Day Name = SUNDAY Day # = 2 Day Name = MONDAY Day # = 3 Day Name = TUESDAY Day # = 4 Day Name = WEDNESDAY Day # = 5 Day Name = THURSDAY Day # = 6 Day Name = FRIDAY Day # = 7 Day Name = SATURDAY |
Links: |
---|
|
See Also: |
Edit Time: 9/20/2020 11:56:31 AM |
Topic ID#: 346 |