Visual Basic FOR loop

The Visual Basic FOR loop or the FOR...NEXT loop is one of the oldest and most frequently used loop structures in programming languages.

Syntax

In VB the FOR...NEXT loop has the following syntax: FOR counter = start to end [Step increment]
    Statements
NEXT [counter]
The FOR loop uses a loop variable (also know as the loop's counter) that increases or decreases in value during each repetition of the loop. Arguments in the above example counter, start, end and increment are all numeric.

Examples

Let us look at an example to understand the Visual Basic FOR loop better.FOR I = 1 to 5
    MsgBox "Current Value of I: " & I
NEXT I
The variable 'I' here is the counter and is incremented by one after each repetition (Note that if the increment argument is not specified the default increment is 1). The repetitions will stop once the value of the counter 'I' goes beyond 5, so if you run the above example you will get 5 MsgBox prompts.



Now let us look at couple of more examples using the increment argument.FOR I = 1 to 5 Step 2
    MsgBox "Current Value of I: " & I
NEXT I

FOR I = 10 to 1 Step -3
    MsgBox "Current Value of I: " & I
NEXT I
In the first example, the increment argument is specified as 2 i.e. the counter variable 'I' will increase by 2 after every repetition. The execution of this example will result in 3 MsgBox prompts (1, 3, 5).

In the second example, notice that the counter variable 'I' starts at 10 and goes down to 1, the increment value is specified as -3 i.e. the variable 'I' keep decreasing by 3 after every repetition. The execution if this example will result in 4 MsgBox prompts (10, 7, 4, 1). For decreasing counter variable one number at a time, you need to specify the increment value as -1

Nested FOR loop

You can also create Nested FOR loops, meaning that you can have FOR…NEXT loop within a FOR…NEXT loop. Here is an example for generating mathematical table from 2 to 10FOR I = 2 to 10
    FOR I2 = 1 to 10
        MsgBox I & "x" & I2 & "=" & I*I2
    NEXT I2
NEXT I
In this example, the inner FOR loop (counter - I2) will repeatedly be executed at every iteration of the outer (counter - I) FOR loop, so with just few lines of code you are able to generate math tables.

Important Exception - Visual Basic FOR Loop

One interesting thing to note on FOR loops is that if you are using variables for specifying end value for the counter, and if you change the value of the variable within the loop, it has absolutely no effect.EndValue = 5
FOR I = 1 to EndValue
    MsgBox "Current Value of I: " & I
    EndValue = 100
NEXT I
This example when executed will result on 5 Msgbox prompts, the new value assigned to 'EndValue' variable within the loop will not have any effect.

FOR Loop More Examples

Here is an example to calculate the average of numbers in an array. The array 'NumArray' is initialized previously with numeric dataFOR I = 0 to UBound(NumArray)
    Total = Total + NumArray(I)
NEXT I
MsgBox "The Average is " & Total / UBound(NumArray)
The next example adds all even numbers from 1 to 100Total = 0
FOR I = 0 to 100 Step 2
    Total = Total + I
NEXT I
MsgBox "The Total is " & Total

Quick Summary

  • The Visual Basic FOR loop requires you to know how many times the statements in the loop needs to be executed. If the number of repetitions of a loop is unknown in advance, then use a Do...Loop or a While...Wend structure.
  • The 'Step' keyword is used to set the increment/decrement valve for each repetation.
  • The Nested Visual Basic FOR loop provide efficient ways to generate complex outputs with minimum lines of code.
Back to Visual Basic Help