Processing Lists of Data with Do Loops (Examples)

28
Reviewing & Processing National Institutes of Health Grant Applications on the PHS- 398 A Guide For Pre-Award Staff: Research & Sponsored Programs University of Wisconsin- Madison May 31, 2006 Stephanie Gray PreAward Supervisor

description

Processing Lists of Data with Do Loops (Examples). 1. Phone Book Enquiry – (Lab sheet 6.3) (Using Peek Method) 2. Find the sum of a series of number. – (Using Counters and Accumulators) 3. Password Checking –(Using Flags) 4. Nested Loops – (Lab sheet 6.6). Lab sheet 6.3: Phone Number Enquiry. - PowerPoint PPT Presentation

Transcript of Processing Lists of Data with Do Loops (Examples)

Page 1: Processing Lists of Data with Do Loops (Examples)

Chapter 6 1

Processing Lists of Data with Do Loops (Examples)

• 1. Phone Book Enquiry – (Lab sheet 6.3)(Using Peek Method)

• 2. Find the sum of a series of number. – (Using Counters and Accumulators)

• 3. Password Checking –(Using Flags)

• 4. Nested Loops – (Lab sheet 6.6)

Page 2: Processing Lists of Data with Do Loops (Examples)

Chapter 6 2

Lab sheet 6.3: Phone Number Enquiry

txtName

txtNumber

First, build the Form with the above layout

btnDisplay

Page 3: Processing Lists of Data with Do Loops (Examples)

Chapter 6 3

What is sr.Peek ?

• Sr.peek is the result of reading the text file.

• It will return the next character, if not available, it will return -1.

Page 4: Processing Lists of Data with Do Loops (Examples)

Chapter 6 4

Lab sheet 6.3 : Phone Book Enquiry(Using Do-While Loop)

Do While (name <> txtName.Text) _

And (sr.Peek <> -1)

name = sr.ReadLine

phoneNum = sr.ReadLine

LoopAs long as the name being searched

for has not been found AND the end of the file has not been reached, the loop

will continue

This make sure there is some text remains in the text file for the program to read.

Page 5: Processing Lists of Data with Do Loops (Examples)

Chapter 6 5

Example : Phone Book Enquiry(Using Do … Loop Until)

Do name = sr.ReadLine phoneNum = sr.ReadLineLoop Until (name <> txtName.Text) _ or (sr.Peek <> -1))

Until the name being searched matches the data OR the end of the file has been reached, the loop will Stop

Page 6: Processing Lists of Data with Do Loops (Examples)

Chapter 6 6

Counters and Accumulators• A counter is a numeric variable that

keeps track of the number of items that have been processed.

• An accumulator is a numeric variable that totals numbers.

• In the following example, variable CurrNum is a counter while Ans is a accumulator.

Page 7: Processing Lists of Data with Do Loops (Examples)

Chapter 6 7

Lab sheet 6.4: Find the sum of a series of number(Using Do-While Loop)

txtstart

btnCalculate

First, build the Form with the above layout

txtend

txtAns

Page 8: Processing Lists of Data with Do Loops (Examples)

Chapter 6 8

Sum of a series of number - Partial Code

Dim EndNo As Double Dim Ans As Double Dim CurrNum As Double CurrNum = txtStart.Text EndNo = txtEnd.Text Ans = 0 Do While CurrNum <= EndNo Ans = Ans + CurrNum CurrNum = CurrNum + 1 Loop txtAns.Text = Ans

Ans is an

accumulator. It is

used to total up

the values.

CurrNum is a counter, it increases by 1 each time through the loop