1 / 8

Processing Lists of Data with Do Loops (Examples)

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.

ishi
Download Presentation

Processing Lists of Data with Do Loops (Examples)

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. 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) Chapter 6

  2. Lab sheet 6.3: Phone Number Enquiry txtName btnDisplay txtNumber First, build the Form with the above layout 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. 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 Loop As long as the name being searched for has not been foundAND 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. Chapter 6

  5. Example : Phone Book Enquiry(Using Do … Loop Until) Do name = sr.ReadLine phoneNum = sr.ReadLine Loop 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 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. Chapter 6

  7. Lab sheet 6.4: Find the sum of a series of number(Using Do-While Loop) txtstart txtend btnCalculate txtAns First, build the Form with the above layout 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 CurrNum is a counter, it increases by 1 each time through the loop Ans is an accumulator. It is used to total up the values. Chapter 6

More Related