Chapter 4 Repetition

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

The Test First Loop

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

PROBLEM

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.

Planning

The Forms and Controls are similar to the previous examples and so will not be listed here.

Pseudocode

Set Total TO 0
Get Number
Do While Number <> 999
  Set Total To Total + Number
  Get Number
EndDo
Write Total

NOTE:

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)

CODING

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

EXERCISE 4 - Complete example 1 & 2 OR 3 & 4 (include Pseudo code)

  1. "Just Friendly" supermarket chain ask you to write a Visual Basic program that will let the checkout operator enter the cost of a customer's grocery items and calculate the total. When -1 is entered, the program calculates the Goods and Services tax rated at 12.5% and adds this to the total to give a final total. The pretax total, amount of tax and the final total are to be displayed.
  1. Modify the example program above to calculate the average of the numbers entered. To calculate the average, the program must count how many numbers are entered. Use a new variable Count and the following code

count = count + 1

to count how many numbers are entered. Don't forget to set count to zero at the start.

  1. A guessing game program is needed to test the intelligence of students. The Test first loop is ideal for this as the user may guess the number first off. Write a computer program that continues to ask the student to guess a number between 1 and 100 until the secret number is guessed. If they guess wrong, display a message "Bad Guess - Try again". When they guess the correct number, display the Message "You guessed it" and stop the program.
  1. Extension to (3). Make your program count the number of guesses taken.

Author Mike Leishman

Last updated: 28 June 1998