Visual Basic Do While Loop
Loops will execute a block of code a number of times, typically until a predefined condition is met. 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 the an entire language (ADA) named after her. Interestingly, her father was a certain Lord Byron. Yes, that Lord Byron, he who inadvertently created Frankenstein. I’m not sure what the biggest monster is in all honesty! Loops are a mainstay of any modern 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 loops 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, “Can you 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 shopping. 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 < 50 i - i + 1 Loop
In this example the code will loop until the Integer variable i reaches 50, then the next line of code is executed.
Lets put this into practise. 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
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 code we see
Do While i < 51
This is how the code executes during the first loop:
- The first time round, i = 0 as we have just declared the variable the line before
- As 0 is less than 51 we enter the body of the loop
- In the body of the loop we add 0 to the listbox
- Add 1 to the value of i. So i = 1.
- Loop.
During the second loop:
- i = 1
- 1 < 51 so enter the body of the loop
- Add 1 to the listbox
- Add 1 to i, so i now = 2
- Loop again.
This continues until i = 51, whereby the loop is no longer executed as 51 is not less than 51, and Visual Basic reads the next line. Which 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 this tutorial, however pen and paper is best sometimes.
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 int he Visual Basic community and we will visit this a lot more in the coming tutorials. Next though, more Loops.