1 / 14

While Loops

While Loops.

homer
Download Presentation

While Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. While Loops

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

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

  4. The While Loop Do While condition ‘do this Loop For example… Dim Counter as Integer Counter = 1 Do While Counter < 10 Counter = Counter + 1 Loop

  5. SwapListed • Notice how the selected item stays selected • Examine the SwapListed Sub Procedure

  6. Some Possible Problems with Do While Loops Dim Counter as Integer Counter = 1 Do While Counter < 10 Counter = Counter - 1 Loop

  7. What about this one? Dim Counter as Integer Counter = 1 Do While Counter < 10 Loop

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

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

  10. Accumulators Consider the following code… Dim Interest As Decimal Dim LoanAmount As Decimal Dim TotalInterest As Decimal Dim LoanMonths As Integer Dim MonthlyPayment as Decimal Dim InterestRate as Decimal InterestRate = 0.004 LoanAmount = 20000 Interest = 0 LoanMonths = 0 MonthlyPayment = txtPayment.Text TotalInterest = 0 Do While LoanAmount >= 0 Interest = LoanAmount * InterestRate TotalInterest = TotalInterest + Interest LoanAmount = LoanAmount - MonthlyPayment + Interest LoanMonths = LoanMonths + 1 Loop

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

  12. Questions 1. What is wrong with the following loops? Dim Counter as Integer Counter = 1 Do While Counter < 10 Counter = Counter - 1 Loop Dim Counter as Integer Counter = 1 Do While Counter < 10 Loop

  13. Questions 2. 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 Integer Counter = 1 Do While Counter < 100 Counter = Counter * 2 lstMyList.Items.Add(Counter) Loop

  14. Questions 3. Write a Do While Loop that loops counting backwards from 10 to 0.

More Related