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 |
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.

Set Total To 0 Do Get Number Set Total To Total + Number While Total < MaxFuel Write Total
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.
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.