Visual Basic For Loop

This third tutorial on loops, following the Do While and the Do Until loop, covers the For Loop.  There are two For Loops in Visual Basic:

  1. The For Next Loop, which you’ll see in virtually every programming language including .NET, Visual Basic 6.0, VB Script and VBA
  2. The For Each loop which was introduced to VB.NET

The For Loop is my favourite of all the loops and one which I use far more than the two Do Loops.   Rather than relying on the state of a condition, the For loops through a sequence.  That sequence may be 1 through to 50, or items in a collection.  With the For Loop you define that sequence and Visual Basic will iterate through it.

Looking at the For Next Loop first, the syntax is as follows:

For number = lower value To higher value

     Code

Next

In practise this looks as follows:

For i As Integer = 1 to 10
     Code
Next

Most loops are executed over some kind of counter.   If you do want to loop through a counter then I would always go for a For Loop as opposed to one of the two Do loops.  The For Loop defines everything you need to know about that loop in one line.  You know exactly where you are with the For Loop the second you read it.   Everything is there to digest without having to look in the body of the loop.  It’s the most honest loop, is straight to the point and that appeals to me.

Look at this same code with the Do While Loop:

Dim i As Integer</span>
<pre>
Do While i < 11
     Code
     i = i + 1
Loop

And with the Do Until Loop:

Dim i As Integer

Do Until i = 11
     Code
     i = i + 1
Loop

In both cases, you have to look at the first line and the body of code to see how the loop works.  You have the condition in the first line of the loop and the counter (in this case i = 1 + 1) in the body of the loop.  It’s much more difficult to read.  The code in both examples works, don’t get me wrong, and working code should be your first priority.  But working code that is easy to read and, hence, easy to maintain, is what you should be aiming for as a craftsman.

You can increment backwards using the For Loop using the following syntax:

For i As Integer = 10 to 1 Step -1
     Code
Next

Or you can increment by more than 1 as so:

For i As Integer = 1 to 10 Step 2
     Code
Next

To put this into practise, create a new Project called “For Loop”.   Add two Listboxes side by side, one called ListBoxNumbers and the other called ListBoxMirror and style them so the Form looks as follows:

for next loop

Put the following code into the Form Load Event:

Dim listItem As Integer

For i As Integer = 0 To 50
     ListBoxNumbers.Items.Add(i)
Next

For Each x As Integer In ListBoxNumbers.Items
     ListBoxMirror.Items.Add(x)
Next

Run this code and you’ll see the two listboxes have exactly the same set of numbers in there.

So what exactly is happening here?

We obviously have two loops here.  The first loop is adding the numbers 1 to 50 to ListBox Numbers using the For Next Loop.

The second one is a lot more interesting and uses the For Each Loop.  The For Each Loop allows you to neatly Loop through every item in a collection.  As ListBoxNumbers contains a collection of Integers, we are reading each item the collection, and adding each item to ListBoxMirror.

Change the code in the first loop to this:

For i As Integer = 50 To 0 Step - 1
     ListBoxNumbers.Items.Add(i)
Next

The numbers in both boxes are reversed.   Whatever is in ListBoxNumbers will be reflected in ListBoxMirror, whatever the contents.   The For Each Loop allows this to happen in an extremely neat manner and we will look at this a lot more in the coming tutorials.

So that concludes the three tutorials on Loops.   Although there’s a lot to absorb, these are some of the fundamentals of programming and mastery of these is essential if you want to be a coding ninja.

The following video brings all of this to life. Let me know what you think!