Do While Loops - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Do While Loops

Description:

A club in Leicester holds exclusive after party sessions for members only. ... The door man takes your membership number, enters it into the computer and the ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 14
Provided by: indi
Category:
Tags: doorman | loops

less

Transcript and Presenter's Notes

Title: Do While Loops


1
Do While Loops
  • For the last few weeks you will have had the
    chance to program
  • For Next loops
  • List boxes

2
The Problem With For Next 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.

3
Pseudo Code
  • Get a count of the number of members
  • store in the variable MemberCount
  • get the membership number of this member
  • store in the variable MemberNo
  • create a Boolean variable MemberFound
  • usume member not found so set MemberFoundFalse
  • 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 MemberFoundtrue then
  • show a message

4
Outline
  • The above pseudo code
  • gets the membership number of the person at the
    door, then
  • searches through each entry in the database to
    see if a match is found.
  • When a match is found, a Boolean flag is set to
    true.

5
The Problem
  • What if there are 300 members in the database,
    the system gets the membership number of the
    person at the door, starts looking for them and
    then finds them on the first record. The big
    problem is that the system will continue looking
    through 299 records unnecessarily as it has found
    the member on the first go.

6
Question
  • Why is something always in the last place you
    look?
  • Answer. Because when you have found it you stop
    looking!
  • The For Next Loop isnt very smart!

7
The Do While Loop
  • may be thought of as a cross between a For Next
    Loop and an If statement.
  • The Syntax is
  • Do While condition
  • do this
  • Loop

8
Example Code
  • Dim Counter as Integer
  • Counter 1
  • Do While Counter lt 10
  • Counter Counter 1
  • Loop

9
Questions
  • What is wrong with the following loops?
  • 1) Dim Counter as Integer
  • Counter 1
  • Do While Counter lt 10
  • Counter Counter - 1
  • Loop
  • 2) Dim Counter as Integer
  • Counter 1
  • Do While Counter lt 10
  • Loop

10
Counters and Accumulators.
  • When we use a loop, we normally want it to
    perform some kind of task or calculation.
  • Common mechanisms used with loops are
  • counters and
  • accumulators.

11
Counters.
  • Counters may be used within loops to answer the
    question How many?
  • Consider the following loop
  • Dim LoanAmount As Decimal
  • LoanAmount 20000
  • Do While LoanAmount gt 0
  • LoanAmount LoanAmount - 157
  • Loop
  • Question, how many payments of 157 are required
    before the loan of 20,000 is paid off?

12
Using a Counter
  • Dim LoanAmount As Decimal
  • Dim PaymentCount As Integer
  • LoanAmount 20000
  • PaymentCount 0
  • Do While LoanAmount gt 0
  • LoanAmount LoanAmount - 157
  • PaymentCount PaymentCount 1
  • Loop
  • Notice that we have added a new variable called
    PaymentCount.

13
Accumulators.
  • Dim InterestPaid As Decimal
  • Dim LoanAmount As Decimal
  • Dim LoanMonths As Integer
  • LoanAmount 20000
  • InterestPaid 0
  • LoanMonths 1
  • Do While LoanAmount gt 0
  • InterestPaid InterestPaid ((LoanAmount
    InterestPaid) 0.004)
  • LoanAmount (LoanAmount - 157) ((LoanAmount
    InterestPaid) 0.004)
  • LoanMonths LoanMonths 1
  • Loop
  • In the above example we are using the variable
    InterestPaid to accumulate the value of
    interest paid for the 20,000 loan.
Write a Comment
User Comments (0)
About PowerShow.com