CS 107 - Lab 5
Loops
In lecture we have discussed the three basic loop statements.
Note that the description given below are the
- while loops - executes the statement zero or more times while
some condition is true.
while ( condition )
statement ;
- for loops - executes the statement for some predetermined
number of times. The init clause specifies some starting
value of the loop variable. THe condition clause
specifies the condition under which the loops keeps executing.
The incr clause specifies how the loop variable should change
after every iteration of the loop.
for ( init ; condition ; incr )
statement ;
- do-while loops - executes the statement one or more times while
some condition is true.
do
statement
while ( condition ) ;
Each loop will keep executing the statement as long as
the condition is true. Any statement can be block
statement to allow for multiple statements to be in the body of the
loop.
The while loops and for loops are the most common loops used. All
for and do-while loops can be written as while loops (the simpliest
form of the looping statements), but rarely does a program
contain only while loops.
Nested Loops
Often a loop will have another loop nested inside of it. These
loops are often called the outer and inner loops. A simple example
of this is to create a multiplication table as follows:
// printout the column headings
cout << " :";
for (i = 1; i <= 10; i++)
{
cout.width(4);
cout << i;
}
cout << endl;
// print out 10 rows with a row heading
// outer loop
for (i = 1; i <= 10; i++)
{
cout.width(4);
cout << i << ":"
// print out the products for the row and column values
// inner loop
for (j = 1; j <= 10; j++)
{
cout.width(4);
cout << i*j;
}
cout << endl;
}
Lab Assignment
Due: By your Lab Time during the sixth week of the semester (the week of
2/17/2003).
You may turn in the assignment to your TA during lab or place it in his
mailbox in 905 SEO. It is suggested that you place it in his mailbox
just in case you are unable to attend lab.
You are to hand in a print out of your program to do the following to your TA.
Create a C++ program on your UIC icarus account that will calculate
the number of months needed to payoff a loan and the amount of interest
paid on the loan. Your program should prompt the user for:
- The total amount of the loan
- The monthly payment
- The monthly interest rate
Your program should first make sure that the monthly payment is
large enough to pay off the monthly interest and some of the debt.
If the (amount of the loan) * (monthly interest rate) >= monthly payment,
then the monthly payment is NOT large enough to payback the loan.
Once you know the monthly payment is large enough, use a loop to
calculate the amount of interest and the size of the debt after each month.
(You may wish to print out these values, but you are not required to.)
Note the last payment may be less and the normal monthly payment amount.
To understand how this is done, let us look at the following example.
Assume you have a loan of $1000, with a monthly payment of $50 and
monthly interest rate of 1.5%.
- For the first month, you will
pay $15 in interest ($1000 * 0.015) and $35 towards the load ($50 - $15)
leaving a debt of $965.00.
- For the second month, you will pay $14.48 in interest ($965.00 * 0.015)
and $35.52 towards the loan ($50 - $14.48) leaving a debt of $929.48.
- Etc.
- The last month, the remaining debt will be small enough such that
the interest on the debt and the debt amount together are less than or
equal to the monthly payment.
Hand in a print out of your program to your TA.