19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Do While there are still data in the file<br />

Get an item of data<br />

Process the item<br />

Loop<br />

FIGURE 5-3 Pseudocode <strong>and</strong> Flowchart for Processing Data from a File<br />

Sequential files can be quite large. Rather than list the entire contents, we typically<br />

search the file for a specific piece of information.<br />

EXAMPLE 2<br />

Modify the program in Example 1 to search the telephone directory for a name specified by the user. If<br />

the name does not appear in the directory, so notify the user.<br />

SOLUTION:<br />

We want to keep searching as long as there is no match <strong>and</strong> we have not reached the end of the list. Therefore,<br />

the condition for the Do While statement is a compound logical expression with the operator And.<br />

After the last pass through the loop, we will know whether the name was found <strong>and</strong> be able to display the<br />

requested information.<br />

Object Property Setting<br />

frmPhone Caption Phone Number<br />

lblName Caption Name to look up<br />

txtName Text (blank)<br />

cmdDisplay Caption Display Phone Number<br />

picNumber<br />

Private Sub cmdDisplay_Click()<br />

Dim nom As String, phoneNum As String<br />

Open “PHONE.TXT”For Input As #1<br />

Do While (nom txtName.Text) And (Not EOF(1))<br />

Input #1, nom, phoneNum<br />

Loop<br />

Close #1<br />

picNumber.Cls<br />

If nom = txtName.Text Then<br />

picNumber.Print nom, phoneNum<br />

Processing Lists of Data with Do Loops 137

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!