Chapter 5 Repetition Test Last

Using Do Loop structure - Test Last Loop

In this case, the loop structure is entered before testing of a condition is done. In other words, the code must be executed at least once. It is important to ensure that the loop can be exited!.

The general form is:

Test Last loop

Do
…statements
Loop While | Until condition

Using Do Loop structure - test last loop

As an introductory offer to all customers of the new Rotnest Fuel Boat refilling station, the customer who purchases fuel during the time the 500th litre is pumped will win a prize. You have been contracted by Rotnest Fuel to write a Visual Basic program that allows the attendant to enter the fuel used by each customer and then announce the time when 500 liters is reached or exceeded. It may look like that below.

PLANNING

Pseudocode

Set Total To 0
Do
  Get Number
  Set Total To Total + Number
While Total < MaxFuel
Write Total

Coding

Private Sub CmdAccumulate_Click()
  Const MaxFuel = 500
  Dim Total As Integer
  Dim Number As Integer
dim strNumber as string
  Total = 0
  Do
    strNumber = InputBox("Enter amount of fuel " & " ; " & "total so far is " & Total)
    Number = CInt(strNumber)'convert string to interger
Total = Total + Number
  Loop While Total < MaxFuel
  MsgBox ("The total amount of fuel sold so far is : " & Total & " Litres")
End Sub

When this program is run, the computer stores 0 in Total.

It then displays the message and Number is entered. The value of Number is added to Total. It then. checks to see whether Number is equal to MaxFuel (500). This part of the program then repeats while Number is less than MaxFuel.

Total is then displayed.

Challenge: After you have this working, add a control and a line of code which says "Congratulations, you are the winner." after the last message box is displayed.

Note that labels have a property called "Visible". This can be set to false at design time, then changed to "True" during the program when a condition is met.

Exercise 5 - Complete 1 of the following.

  1. JOJO disco can only hold 1000 people. The customers usually come in groups. Write a program that will allow the JOJO door-keeper to enter the number in each group and tell him when the limit has been exceeded.
  2. Bunnys hardware needs a program that will allow stock control of a paint sale where 80 cans of paint are available. Write a program that will allow the check-out operator to enter the number of cans purchased and advise when no more paint is left.
  3. Random Numbers Inc wants a program that will simulate a die being thrown (the numbers are from 1 - 12). The idea is that the user enters the number they want to throw and then bet on how many throws it takes to get the number. You have been contracted to write this progam in Visual Basic. You will need to use the code

Dice1 = Int(12 * Rnd) + 1

to simulate the die being thrown and is activated by a command button. Your program will continue to allow the die to be thrown until the selected number is obtained, and then report how many throws it took to get it.