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.

138 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

Else<br />

picNumber.Print “Name not found.”<br />

End If<br />

End Sub<br />

[Run, type Grover into the text box, <strong>and</strong> press the comm<strong>and</strong> button.]<br />

■ COUNTERS AND ACCUMULATORS<br />

A counter is a numeric variable that keeps track of the number of items that have been<br />

processed. An accumulator is a numeric variable that totals numbers.<br />

EXAMPLE 3<br />

The following program counts <strong>and</strong> finds the value of coins listed in a file.<br />

COINS.TXT contains the following entries: 1, 1, 5, 10, 10, 25<br />

Object Property Setting<br />

frmCoins Caption Coins<br />

cmdAnalyze Caption Analyze Change<br />

picValue<br />

Private Sub cmdAnalyze_Click()<br />

Dim numCoins As Integer, sum As Single, value As Single<br />

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

numCoins = 0<br />

sum = 0<br />

Do While Not EOF(1)<br />

Input #1, value<br />

numCoins = numCoins + 1<br />

sum = sum + value<br />

Loop<br />

picValue.Cls<br />

picValue.Print “The value of the”; numCoins; “coins is”; sum; “cents.”<br />

Close #1<br />

End Sub<br />

[Run, <strong>and</strong> press the comm<strong>and</strong> button.]<br />

The value of the counter, numCoins, was initially 0 <strong>and</strong> changed on each execution of the loop<br />

to 1, 2, 3, 4, 5, <strong>and</strong> finally 6. The accumulator, sum, initially had the value 0 <strong>and</strong> increased with<br />

each execution of the loop to 1, 2, 7, 17, 27, <strong>and</strong> finally 52.

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

Saved successfully!

Ooh no, something went wrong!