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.

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

If UCase(txtName.Text) = “SECRET.TXT”Then<br />

Do<br />

passWord = InputBox(“What is the password?”)<br />

passWord = UCase(passWord)<br />

Loop Until passWord = “SHAZAM”<br />

End If<br />

Open txtName.Text For Input As #1<br />

Input #1, info<br />

picItem.Cls<br />

picItem.Print info<br />

Close #1<br />

End Sub<br />

Do loops allow us to calculate useful quantities for which we might not know a simple<br />

formula.<br />

EXAMPLE 4<br />

Suppose you deposit $100 into a savings account <strong>and</strong> let it accumulate at 7 percent interest compounded<br />

annually. The following program determines when you will be a millionaire.<br />

Object Property Setting<br />

frmInterest Caption 7% Interest<br />

lblAmount Caption Amount Deposited<br />

txtAmount Text (blank)<br />

cmdYears Caption Years to become a<br />

millionaire<br />

picWhen<br />

Private Sub cmdYears_Click()<br />

Dim balance As Single, numYears As Integer<br />

‘Compute years required to become a millionaire<br />

picWhen.Cls<br />

balance = Val(txtAmount.Text)<br />

numYears = 0<br />

Do While balance < 1000000<br />

balance = balance + .07 * balance<br />

numYears = numYears + 1<br />

Loop<br />

picWhen.Print “In”; numYears; “years you will have a million dollars.”<br />

End Sub<br />

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

COMMENTS<br />

1. Be careful to avoid infinite loops—that is, loops that are never exited. The following<br />

loop is infinite, because the condition “num < > 0”will always be true.<br />

Note: The loop can be terminated by pressing Ctrl+Break.

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

Saved successfully!

Ooh no, something went wrong!