While...Wend statements - a shop till
Back to Main Page
We have already used For ...Next statements - they are very useful if we know how many times a loop needs to be executed.
What will happen if we
don't know at the start how many iterations are needed?
For this we will use a While...Wend statement. Think of the word
Wend to mean While End.


A good example of this situation is a shop till. The prices of items are entered and the total is only displayed
when all of the items are counted. Some shoppers may buy 1 item, others may buy 20 items - we won't know until the last item is processed.


We have to find a way of telling the program when to end the While..Wend loop. To do this we use something called a
sentinel value. When VB sees that the user has typed in the sentinel value it ends the while loop.


For our shop till, we will use zero as the sentinel value. The algorithm will work as follows:

- The program asks the user to enter the price of an item
- The user types in the price
- The price is added to the 'running total'
- The user is asked to enter the next price
- The price is added to the 'running total'
- This continues until all of the items of shopping are processed
- The user then enters 0 as the final price
- The program then displays the total cost of the shopping

In this case entering a value of 0 will be the same as pressing the 'Total' button on the shop till.


Task 1

Enter the code below and check that the till works correctly
You might be a little confused why the line

NextPrice = InputBox("Please enter the price of item or 0 to end")

appears twice in the code - couldn't it just sit once inside the While.....Wend loop?

In fact this wouldn't work - we would never enter the while loop VB would jump to
Form1.Show and a total of zero would be printed straight away!

Before we enter the loop we have to ask for the
first price, so that the While statement has a value to check.
Then we put this price as the first value in the loop. The loop checks to see that it isn't 0 and adds the value to the total. Then we ask the user for the next price and loop around again, checking for that sentinel 0 and ending when 0 is entered.
Task 2

Write a program that will count how many people passed and how many failed their driving test.

The user should enter the following responses:
'P' for passed
'F' for failed
'Q' to quit when all results are entered

The algorithm should:

- Ask the user for the result - P, F or Q
- Add the result to the total passed (or total failed depending on the result)
- End when the letter Q is entered
- Display the number of people who have passed and failed the test

Hints:
1. Remember that we are asking the user to enter a letter here, not a number.
   This will mean that we need to declare the user's response as a STRING   
    i.e. Dim Response as String
2. When we test the value of a string, we must put it inside " " marks, for example, we write
    While Response <> "Q"


End of tasks
Hosted by www.Geocities.ws

1