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.

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

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

COMMENTS<br />

1. The body of a For...Next loop need not be indented. However, because indenting<br />

improves the readability of the program, it is good programming style. As<br />

soon as you see the word For, your eyes can easily scan down the program to<br />

find the matching Next statement. You then know two facts immediately: the<br />

number of statements in the body of the loop <strong>and</strong> the number of passes that will<br />

be made through the loop.<br />

2. For <strong>and</strong> Next statements must be paired. If one is missing, the program will<br />

generate the error message “For without Next”or “Next without For.”<br />

3. Consider a loop beginning with For i = m To n Step s. The loop will be executed<br />

exactly once if m equals n no matter what value s has. The loop will not be<br />

executed at all if m is greater than n <strong>and</strong> s is positive, or if m is less than n <strong>and</strong><br />

s is negative.<br />

4. The value of the control variable should not be altered within the body of the<br />

loop; doing so might cause the loop to repeat indefinitely or have an unpredictable<br />

number of repetitions.<br />

5. Noninteger step values can lead to roundoff errors with the result that the loop<br />

is not executed the intended number of times. For instance, a loop beginning<br />

with For i = 1 To 2 Step .1 will be executed only 10 times instead of the intended<br />

11 times. It should be replaced with For i = 1 To 2.01 Step .1.<br />

5.4 A CASE STUDY: ANALYZE A LOAN<br />

This case study develops a program to analyze a loan. Assume the loan is repaid in equal<br />

monthly payments <strong>and</strong> interest is compounded monthly. The program should request the amount<br />

(principal) of the loan, the annual rate of interest, <strong>and</strong> the number of years over which the loan<br />

is to be repaid. The four options to be provided by comm<strong>and</strong> buttons are as follows.<br />

1. Calculate the monthly payment. The formula for the monthly payment is<br />

payment = p * r / (1 – (1 + r) ^ (–n))<br />

where p is the principal of the loan, r is the monthly interest rate (annual rate<br />

divided by 12) given as a number between 0 (for 0 percent) <strong>and</strong> 1 (for 100 percent),<br />

<strong>and</strong> n is the number of months over which the loan is to be repaid.<br />

Because a payment computed in this manner can be expected to include fractions<br />

of a cent, the value should be rounded up to the next nearest cent. This corrected<br />

payment can be achieved using the formula

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

Saved successfully!

Ooh no, something went wrong!