Sometimes you find that you want the computer to repeat the same set of instructions over and over again. Rather than retype them each time, a loop tells the computer to keep repeating them a certain number of times and then stop.
There are a number of different styles of loop, each suited best to certain situations.
Do While Loop
A Do while loop might look like this:
Do While shopkeeper is not looking Eat lolly Loop |
When the computer sees a Do While loop, it works out whether the condition (shopkeeper is not looking) is true or false.
If the condition is true, it follows the instructions (Eat lolly)
If, or when, the condition is false, (ie shopkeeper is looking) then it will ignore all the instructions in the loop and move on in the program.
So lollies will be eaten until the shopkeeper looks, then no more will be eaten!
Look carefully at this code:
Sub askName()
End Sub |
The first line deletes any information stored in userName. The purpose of the loop (in bold) is to ensure that the user has entered a name. While the userName is blank, (ie until the user enters a name) the computer will keep popping up an Input Box asking for a name.
You could also add in this line inside your loop:
Beep
This would play a system Beep if the user has not entered in a name.
![]() |
|


