Most programs need some process to be repeated many times. A process usually needs many lines of code. To avoid having to write that part of the code many times, the Repeat Structure is used. This comes in three basic forms
Getting a part of a program to repeat using Do..Loop or Test First Loop structure
The Test First Loop gives the option to enter into a code segment, and continue using this code until a condition is met. The coade may never be activated if the appropriate condition is met.
The general form is:
|
Test first Loop |
| Do While | Until condition … statements Loop |
Condition controls the outcome and must be a Boolean expression.
An example would be Age < 6 where Age is a Variable, < is a boolean operator and 6 is a test value.
Boolean Operators
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| = | Equal to |
| <> | Not equal to |
A program is needed that will add, if necessary, a set of numbers. To determine when all of the required numbers have been entered, the number 999 is entered. This is not included in the addition. The total of the entered numbers is then displayed. The number 999 is called a sentinal.
The initial value for the total must be set to zero, and the user asked to enter a number to add. A Test First Loop solution is used to determine if a 999 has been entered. If not, the number is added to the total. The pseudocode is shown below.

The Forms and Controls are similar to the previous examples and so will not be listed here.
Set Total TO 0 Get Number Do While Number <> 999 Set Total To Total + Number Get Number EndDo Write Total
An easy way of icnorporating a message and a method of inputing data is by use of the InputBox statement. The form is as follows: Variable = InputBox(Message) This retuns a string variable. So to input the users name you might
dim name as string
name = InputBox("Please enter your name.")
This works fine until you want to input a number. Now sometimes it seems to work, but more often than not it crashes, and once this occurs you really have to start the project again from scratch as "repairs" don't work. Use the CInt function to convert a string to integer.
dim strAge as string
Dim age as integer
strAge = InputBox("Please enter your age.")
age = CInt(strAge)
MsgBox("Your age is " & age)
Private Sub Command2_Click()
'Comments
Dim Total As Integer
Dim Number As Integer
strNumber as string
Total = 0
strNumber = InputBox("Enter a number - 999 to finish")
number = CInt(strNumber)
Do While Number <> 999
Total = Total + Number
LblOut.text = Total
Number = InputBox("Enter a number - 999 to finish")
Loop
End Sub
When this program is run, the computer stores 0 in Total and then displays the message and Number is entered. It is important to note that the value for Number must be obtained before the Loop test is carried out.
It then checks to see if Number is not equal to 999. If it is not, Number is added to Total. It then puts total into the Label and Number is entered again. This part of the program then repeats until 999 is entered. The final value for the Total is displayed.
An alternate method is to use the structure Do …… Until Number = 999
count = count + 1
to count how many numbers are entered. Don't forget to set count to zero at the start.
Author Mike Leishman
Last updated: 28 June 1998