Section 8: Program Branching and Controls 95
Example: Flags
Calculations on debts or investments can be calculated in two ways: for
payments made in advance (at the beginning of a given period) and for
payments made in arrears (at the end of a given period). If you write a
program to calculate the value (or “present value”) of a debt or
investment with periodic interest and periodic payments, you can use a
flag as a status indicator to tell the program whether to assume payments
are made in advance or payments are made in arrears.
Suppose you are planning the payment of your child’s future college
tuition. You expect the cost to be about $3,000/year or about
$250/month. If you wanted to withdraw the monthly payments from a
bank account yielding 6% per year, compounded monthly (which equals
0.5% per month), how much must you deposit in the account at the start
of the college years to fund monthly payments for the next 4 years?
The formula is
V = P
1 −
1 + i
−n
i
1
+
i
if payments are to be made
each month in advance,
and the formula is
V = P
1
−
1
+
i
−n
i
if payments are to be
each month in arrears.
V is the total value of the deposit you must make in the account;
P is the size of the periodic payment you will draw from the account;
i is the periodic interest rate (here: “periodic” means monthly, since
interest is compounded monthly); and
n is the number of compounding periods (months).
The following program allows for either payment mode. It assumes that,
before the program is run, P is in the Z-register, n is in the Y-register, and
i is in the X-register.