while loops
DESCRIPTION
While Loops. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/1.jpg)
![Page 2: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/2.jpg)
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.
![Page 3: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/3.jpg)
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
![Page 4: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/4.jpg)
The While LoopDo While condition
‘do thisLoop
For example…
Dim Counter as IntegerCounter = 1Do While Counter < 10
Counter = Counter + 1Loop
![Page 5: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/5.jpg)
SwapListed
•Notice how the selected item stays selected
•Examine the SwapListed Sub Procedure
![Page 6: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/6.jpg)
Some Possible Problems with Do While Loops
Dim Counter as IntegerCounter = 1Do While Counter < 10
Counter = Counter - 1Loop
![Page 7: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/7.jpg)
What about this one?
Dim Counter as IntegerCounter = 1Do While Counter < 10Loop
![Page 8: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/8.jpg)
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?
![Page 9: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/9.jpg)
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?
![Page 10: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/10.jpg)
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
![Page 11: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/11.jpg)
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
![Page 12: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/12.jpg)
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
![Page 13: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/13.jpg)
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
![Page 14: While Loops](https://reader035.vdocument.in/reader035/viewer/2022070403/56813992550346895da1290d/html5/thumbnails/14.jpg)
Questions3. Write a Do While Loop that loops counting
backwards from 10 to 0.