VB.NET For loop

This article covers the use of the VB.NET For Loop.

The VB.NET for loop is used to create a loop that runs a certain amount of times, until the given condition has been met. This is in contrast to its loop counter parts, the while loop and Do loop Until which iterate for an unspecified number of times as long as a condition is met.

The For loop also has another variant called the VB.NET For Each loop. It is used to iterate over sequences of data. The number of times this loop repeats depends on the number of elements in the sequence.

Make sure to use the correct loop for your program. Evaluate the pros and cons of each loop and be clear on what you require.


Defining the For Loop

For counter [ As datatype ] = start To end [ Step step ]
    'Statements to be executed
    .
    .
Next 
  • counter: Required. the control variable for this for loop. The counter name is just a placeholder, it can have any name.
  • As datatype: Optional. This defines the data type of the counter variable. If not included, you will have to define the counter data type yourself.
  • start: Required. This is the initial value of counter.
  • end: Required. This is the final value of counter.
  • step: Optional. You can change the increment value of counter after each iteration. Default increment is 1.
  • Next: Required. Increments the loop, moving it to the next iteration.

Example 1

A simple example of a for loop that prints out values from 1 to 5.

Module Module1
    Sub Main()

        For count As Integer = 1 To 5
            Console.WriteLine(count)
        Next

    End Sub
End Module
1
2
3
4
5

Alternatively, you could have written the code like this. There is no difference in the execution of the code, or it’s output.

Module Module1
    Sub Main()
        Dim count As Integer

        For count = 1 To 5
            Console.WriteLine(count)
        Next

    End Sub
End Module

Example 2

The following is another variant of the above code, but with the inclusion of the Step feature. You can use this create loops that print out all odd or even numbers in a given range.

Module Module1
    Sub Main()

        For count As Integer = 1 To 10 Step 2
            Console.WriteLine(count)
        Next

    End Sub
End Module
1
3
5
7
9

Exit For and Continue For

The Exit For statement immediately exits the ForNext loop and transfers control to the statement that follows the Next statement. The Continue For statement transfers control immediately to the next iteration of the loop, ending the current iteration.

The following examples demonstrates the use of the Continue For and Exit For statements in VB.NET.

Module Module1
    Sub Main()

        For count As Integer = 1 To 5
            If count = 4 Then
                Continue For
            End If
            Console.WriteLine(count)
        Next

    End Sub
End Module

As expected, in the output of this code, 4 is missing as the continue statement skipped over it.

1
2
3
5

Let’s try the same thing, but with Exit For instead now.

Module Module1
    Sub Main()

        For count As Integer = 1 To 5
            If count = 4 Then
                Exit For
            End If
            Console.WriteLine(count)
        Next

    End Sub
End Module

Unlike the Continue For which merely skips the iteration where 4 is displayed, the Exit For stops the whole loop at iteration 4.

1
2
3

This marks the end of the VB.NET For loops article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked below in the comment section.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments