Visual Basic Do While Loop

So far in these tutorials, we have executed each line of code once and once only.  Loops, as the name suggests, allow us to instruct Visual Basic to execute a line (or many lines) multiple times.   They are credited to have been invented by Countess Ada Lovelace when she was experimenting with Babbage’s “Analytical Engine”.  She is widely acknowledged to have developed the first computer program, with loops, subroutines and all.  It’s no wonder she has an entire language (ADA) named after her.  Interestingly, her father was a certain Lord Byron.    Yes, the Lord Byron who inadvertently created Frankenstein.  Talk about an interesting history!   Back to the modern day and loops are a mainstay of any programming language, which not surprisingly includes Visual Basic, and look as follows:

Start loop

     Code

End loop when some condition is met

There are three main types of loop in Visual Basic which are covered in the next few tutorials.  The first loop we’ll be covering is perhaps the simplest to digest, the Do While Loop.

The Do While Loop is an instruction I hear daily from my girlfriend.  Do something While I go out shopping.   Typically this has the syntax, “Look after the baby while I go out shopping”.   In this instance, I am stuck in the seemingly infinite loop of looking after the lovable little one while she is out galavanting.  In theory I should, by definition, stop looking after the little one (or exit the loop) when she returns from shopping.  This rarely happens in reality but thankfully Visual Basic is true to its word.

The basic structure of a Do While Loop is as follows:

Do while some condition is true

     block of code 

Loop

In my girlfriends example, this is (theoretically):

Do While she is out shopping

    Look After Baby

Loop

A more practical example looks like:

Dim i as Integer

i = 0

Do While i < 51

     i - i + 1

Loop

In this example the code “i=i+1” will loop until the Integer variable i reaches 50.

Let’s create one for ourselves.  Create a New Project called “Do While Loop”

Add a Listbox to your form, call it ListBoxNumbers and resize it so it looks roughly as follows:

Do While Loop

Double click on the Form and in the Form Load event, add the following code.


Dim i As Integer

Do While i < 51
     ListBoxNumbers.Items.Add(i)
     i = i + 1
Loop

Run your application and you should see the Listbox populated with 50 sequential numbers.  So what’s going on here?

The main body of code is saying:

Do While i is less than 51
     Add i to the listbox
     increment (add to) the value of i by one
Loop

Imagine we are Visual Basic and we are executing the code. The first time we reach the loop we see the line:

Do While i < 51

This is how the code executes during the first loop:

  1. The first time round, i = 0
  2. As 0 is less than 51 we enter the body of the loop
  3. In the body of the loop we add 0 to the listbox
  4. We then add 1 to the value of i.  So i = 1
  5. Loop

When executing the second loop:

  1. i = 1
  2. 1 < 51 so enter the body of the loop
  3. Add 1 to the listbox
  4. Add 1 to i, so i now = 2
  5. Loop again

This continues until i = 51, whereby the loop is no longer executed as 51 is not less than 51.  At this point Visual Basic Visual Basic reads the next lines of code. In this case, this happens to be the end of the routine.

Hopefully that makes sense. It often helps to break down the execution of the code like that.  There are debugging tools available which I’ll go over later in these tutorials; however pen and paper is best on occasion.

Another potential gotcha in this code is the ListBox.Items.Add( i ) line of code as we haven’t worked with Listboxes before. A Listbox displays a collection of items. To add items to the collection, you use the Items.Add, and you can add numbers or text. It is a very simple but powerful control much loved in the Visual Basic community and we will visit this a lot more in the coming tutorials. Next though, more Loops.

To see this tutorial in action view the following video