For Loops in VB .NET

We've produced a video to go with this lesson. It's recommended that you read the text below as well, though. The video is here:

Programming Loops

This first type of loop we'll look at is called a For Loop. It is the most common type of loop you'll use when you programme. We'll use one to add up our 4 numbers, and then discuss the code. Study the following. In fact, create a new Project. Add a button to your new Form. Double click your new button and type the following code for it:

Dim answer As Integer
Dim startNumber As Integer

answer = 0

For startNumber = 1 To 4

answer = answer + startNumber

Next startNumber

MessageBox.Show( answer )

Run the programme, and see what happens when you click the button. The number 10 should have been displayed in your message box.

 

The For loop code

We start by setting up two integer variables. We set one of these to zero. Then we start our loop code. Let's examine that in more detail.

For startNumber = 1 To 4

answer = answer + startNumber

Next startNumber

We start our loop by telling Visual Basic what type of loop we want to use. In this case it is a For loop:

For startNumber = 1 To 4

The next thing you have to do is tell Visual Basic what number you want the loop to start at:

For startNumber = 1 To 4

Here we are saying "Start the loop at the number 1". The variable startNumber can be called anything you like. A popular name to call a start loop variable is the letter i ( i = 1). So what we're doing is setting up a variable - the start of the loop variable - and putting 1 into it;

Next, you have to Tell Visual Basic what number to end the loop on:

For startNumber = 1 To 4

The To word, followed by a number or variable, tells Visual Basic how many times you want the loop to go round and round. We're telling Visual Basic to loop until the startNumber variable equals 4

The command that tells Visual basic to grab the next number in the sequence is this:

Next startNumber

When Visual Basic reaches this line, it checks to see what is in the variable startNumber. It then adds one to it. In other words, "Get me the next number after the one I've just used."

The next thing that happens is that Visual Basic will return to the word For. It returns because it's in a loop. It needs to know if it can stop looping. To check to see if it can stop looping, it skips the startNumber = 1 part, and then jumps to your end number. In our case, the end number was 4. Because Next startNumber adds one to whatever is in startNumber, then startNumber is now 2 (It was 1 at the start. The next number after one is ... ?).

So if startNumber is now 2, can Visual Basic stop looping? No it can’t. Because we’ve told it to loop until it reaches number 4. It’s only reached number 2, so off it goes on another trip around the loop. When the startNumber is greater than the end number, Visual Basic drops out of the loop and continues on it’s way.

But remember why we're looping: so that we can execute some code over and over again.

To clarify things, change the above code to this:

Dim startNumber As Integer

For startNumber = 1 To 4

MessageBox.Show("Start Number = " & startNumber)

Next startNumber

Run the programme, and click your button. What happens? You should have seen this in the message box, one after the other:

Start Number = 1
Start Number = 2
Start Number = 3
Start Number = 4

Each time round the loop, the code for the message box was executed. You had to click OK four times - startNumber = 1 To 4.

 

 

Summary

So to sum up:

  1. A For loop needs a start position and an end position, and all on the same line
  2. A For loop also needs a way to get the next number in the loop
  3. A loop without any code to execute looks like this:

For i = startNumber To endNumber

Next i

The above code uses two variables for the start and end numbers. The start number for the loop goes directly into the variable called i. When Visual Basic wants the next number, it just add one to whatever is in the variable i. You could use it like this:

Dim startNumber As Integer
Dim endNumber As Integer
Dim i As Integer

startNumber = 1
endNumber = 4

For i = startNumber To endNumber

MessageBox.Show( i )

Next i

Change the code for your button to that new code, and test it out. Study the code so that you understand what is going on.

For Loops might not be easier to understand than just typing answer = 1 + 2 + 3 + 4, but they are a lot more powerful if you want to add up a thousand numbers!

 

Exercise

Put two textboxes on your form. The first box asks users to enter a start position for a For Loop; the second textbox asks user to enter an end position for the For loop. When a button is clicked, the programme will add up the numbers between the start position and the end position. Display the answer in a message box. You can use this For Loop code

For i = startNumber To endNumber

answer = answer + i

Next i

Get the startNumber and endNumber from the textboxes.

 

Exercise H

Amend your code to check that the user has entered numbers in the textboxes. You will need an If statement to do this. If there's nothing in the textboxes, you can halt the programme with this code

Exit Sub

For this exercise, you will be passing whatever is in the textboxes to integer variables. It is these variables you are checking with your If Statement. Because numbers will be entered into the textboxes, remember to convert the text to a value with Val( ).

But the Text property will return a zero if the box is empty. So your If statement will need to check the variables for a value of zero. If it finds a zero, Then you can use the Exit Sub code. The If statement should come first, before the For Loop code.

Answer to Exercise H (Both exercises above)

 

This has been quite a long section, and you may need a breather! Don't worry if you don't understand all that in one sitting. Come back to it, and it will sink in - eventaully! In the next section, we'll take a look at Do Loops.