For...Next Statements

These instructions bound a block of instructions that are repeatedly executed a specified number of times.

For variable = initial_value To final_value Step increment
     [statements]
Next variable2

Prerequisites

None

Parameters

variable

Required control variable that is incremented each loop and whose value determines when looping is to be terminated. The variable can be any numeric type, i.e.. Byte, Integer, Short, Single or Double. Array variables as well as object and structure fields are also permitted. However, object and structure properties are not permitted.

initial_value

Required expression that is evaluated once when the For loop is first entered. The variable is set to this initial_value and has this value at the start of the first pass through the execution of the statements.

final_value

Required expression whose value is tested against the variable to determine when loop execution is to terminate. This expression is evaluated once when the For statement is executed and its value is saved for subsequent tests by the Next statement. Therefore, this value will not change once the For loop is entered.

increment

Optional expression that determines the amount by which the variable is changed each loop and also whether the variable is tested for being greater than or less than the final_value as the termination condition. This expression is evaluated once when the For statement is executed and its value is saved for subsequent tests by the Next statement. Therefore, this value will not change once the For loop is entered. If this expression is not specified, a step of 1 is assumed.

statements

Optional statement or list of statements that are repeatedly executed during each For loop.

variable2

Optional control variable, which if specified, must exactly match the control variable in the matching For statement. This is only used when the program is compiled (and not at runtime) to ensure that the Next and For statements match.

Remarks

This control structure loops and repeatedly executes the statements a specified number of times (iterations). It can be used to implement program instruction loops and is generally more efficient that the other means of looping.

The For statement begins execution by evaluating its arguments and saving their values for future potential use by the matching Next statement. It then sets the value of the control variable equal to the initial_value. If the variable’s value does not exceed the final_value, then the statements are executed for the first time. If the variable’s value does exceed the final_value, the statements are skipped and execution continues at the first statement beyond the matching Next.

If the statements are executed, execution proceeds until the Next instruction is encounter. When the Next statement is executed, the increment is added to the variable and its value is compared again to the final_value. So long as the final_value is not exceeded, the for_loop_statements are executed again and the process is repeated. Otherwise, execution continues at the statement following the Next.

If the increment is a positive number, looping terminates when the variable’s value is greater than the final_value. If negative, looping terminates when the variable’s value is less than the final_value.

For more complex logic, multiple For…Next sequences can be nested to an arbitrary depth and can be combined with other nested control structures. For example, a For loop can contain an If…Then…End If sequence which can in turn contain another For…Next sequence.

Execution of the For loop can be terminated by a number of different methods: the variable’s value can exceed the final_value; execution can be explicitly transferred to an instruction outside of the loop, e.g. by the execution of a GoTo instruction; or an Exit For instruction can be executed.

When an Exit For statement is encountered, execution of the innermost For…Next sequence is immediately terminated and execution continues at the instruction following the Next. There can be none or several Exit For statements within each For loop.

Examples

Dim count As Integer 
For
count = 1 To 10 ' Plan to execute 10 loops
If count = 5 Then
Exit For ' Prematurely stops For on 5th loop
End If
Next count ' count is optional in the Next

See Also

Statements |Do… Loop Statements| GoTo Statements | If…Then…Else…End If Statements| While…End While Statements