while loops

14

Upload: homer

Post on 04-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

While Loops. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: While Loops
Page 2: While Loops

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

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

The While LoopDo While condition

‘do thisLoop

For example…

Dim Counter as IntegerCounter = 1Do While Counter < 10

Counter = Counter + 1Loop

Page 5: While Loops

SwapListed

•Notice how the selected item stays selected

•Examine the SwapListed Sub Procedure

Page 6: While Loops

Some Possible Problems with Do While Loops

Dim Counter as IntegerCounter = 1Do While Counter < 10

Counter = Counter - 1Loop

Page 7: While Loops

What about this one?

Dim Counter as IntegerCounter = 1Do While Counter < 10Loop

Page 8: While Loops

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

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

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

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

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

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

Questions3. Write a Do While Loop that loops counting

backwards from 10 to 0.