while loops
DESCRIPTION
While Loops. - PowerPoint PPT PresentationTRANSCRIPT
A club in Leicester holds exclusive after party sessions for members only. In order to get into one of the after parties you need to be “on the list”. The club has recently introduced a computerised system for handling this process. The door man takes your membership number, enters it into the computer and the program checks to see if you are able to come in.
Is this an acceptable solution?‘Get a count of the number of members‘store in the variable MemberCount‘get the membership number of the member to search for‘store this in the variable MemberNo‘create a Boolean variable MemberFound‘assume member not found so set MemberFound=False‘now look through every single member‘for loop = 1 to MemberCount
‘get membership No for a member in the database‘store in the variable ExistingMemberNo‘if the MemberNo of the person at the door‘matches the ExistingMemberNo in the database‘then they are “On the List”‘if they are on the list then flag found
‘MemberFound = true‘end if‘move onto the next item in the database
‘keep looping to the end‘if MemberFound=true then
‘show a message‘else
‘state they are not on the list‘end if
The While LoopDo While condition
‘do thisLoop
For example…
Dim Counter as IntegerCounter = 1Do While Counter < 10
Counter = Counter + 1Loop
SwapListed
•Notice how the selected item stays selected
•Examine the SwapListed Sub Procedure
Some Possible Problems with Do While Loops
Dim Counter as IntegerCounter = 1Do While Counter < 10
Counter = Counter - 1Loop
What about this one?
Dim Counter as IntegerCounter = 1Do While Counter < 10Loop
Counters Dim LoanAmount As Decimal LoanAmount = 20000 Do While LoanAmount >= 0 LoanAmount = LoanAmount - 157 Loop
Question, how many payments of £157 are required before the loan of £20,000 is paid off?
Counters Dim LoanAmount As Decimal
Dim PaymentCount As Integer LoanAmount = 20000 PaymentCount = 0 Do While LoanAmount >= 0 LoanAmount = LoanAmount - 157 PaymentCount = PaymentCount + 1 Loop
Question, how many payments of £157 are required before the loan of £20,000 is paid off?
AccumulatorsConsider the following code…
Dim Interest As DecimalDim LoanAmount As DecimalDim TotalInterest As DecimalDim LoanMonths As IntegerDim MonthlyPayment as DecimalDim InterestRate as Decimal
InterestRate = 0.004LoanAmount = 20000Interest = 0LoanMonths = 0MonthlyPayment = txtPayment.TextTotalInterest = 0
Do While LoanAmount >= 0 Interest = LoanAmount * InterestRate TotalInterest = TotalInterest + Interest LoanAmount = LoanAmount - MonthlyPayment + Interest LoanMonths = LoanMonths + 1Loop
Revised night club code
‘Declare a variable called Count to count how far we have got‘Get a count of the number of members ‘store in the variable MemberCount‘get the membership number of the member to search for‘store this in the variable MemberNo‘create a Boolean variable MemberFound‘assume member not found so set MemberFound=False‘now look through members until the member is found‘While Count <= MemberCount and MemberFound=False
‘get membership No for a member in the database‘store in the variable ExistingMemberNo‘if the MemberNo of the person at the door‘matches the ExistingMemberNo in the database‘then they are “On the List”‘if they are on the list then flag found
‘MemberFound = true‘end if‘move onto the next item in the database
‘End ‘if MemberFound=true then
‘show a message‘else
‘state they are not on the list‘end if
Questions1. What is wrong with the following loops? Dim Counter as IntegerCounter = 1Do While Counter < 10
Counter = Counter - 1Loop Dim Counter as IntegerCounter = 1Do While Counter < 10Loop
Questions2. What values will the following Do While loops output to the list box. Dim Counter As Integer Counter = 5 Do While Counter >= 0 Counter = Counter - 1 lstMyList.Items.Add(Counter) Loop Dim Counter as IntegerCounter = 1Do While Counter < 100
Counter = Counter * 2lstMyList.Items.Add(Counter)
Loop
Questions3. Write a Do While Loop that loops counting
backwards from 10 to 0.