 |
Designing loops
|
Reading: Chapter 9.1-9.3.11
-
Like designing classes, there is a method for defining loops that can be
helpful
-
Informal procedure
-
Choosing and defining variables
-
The while sketch
-
The while condition
-
Initialization
-
Guaranteeing termination
-
Completing the loop body
-
Initialization (again)
Multiplication
Say there was no multiplication operations, and we needed to write a method
to provide this operation:
private int multiply (int x, int y) {...}
Informal procedure
-
Observe that multiplication is just repeated addition
x * y = 0 + x + x + x + ... + x
x added to 0, y times
-
Note: using 0 so that multiplication works for the case that y is 0
-
So, informal procedure is:
-
start with 0 and add x to it, then add x to the result, then add x to that
result, until we've added it y times. The last result is the value to return
Choosing and defining variables
-
Need to keep track of the result, and the number of additions so far. This
suggests two variables:
int count; // count == the number of additions done
int result; // result == 0 + x + x + ... + x, i.e., result == x * count
Skeleton of the code
private int multiply (int x, int y) {
int count; // count == the number of additions done
int result; // result == 0 + x + x + ... + x, i.e., result == x * count
...
while (condition) {
body
}
...
}
The while condition
-
count contains the number of additions so far; thus, at the end of the
loop we want count == y.
-
we therefore want to continue the loop as long as count is not equal to
y
-
at the end of the loop, result will contain the value we wish to return
while (count != y) {
body
}
//count == y, result == x * y
return result;
Initialization
-
according to our design so far, count contains the number of times that
we have added so far
-
before the loop has executed once, this is 0, so count should be 0
Guaranteeing termination
-
So far, the while statement we have written will not terminate
-
In the loop body, we need to increment count by 1 each time through the
loop
-
This makes sense because
-
one more time through the loop means one more addition
-
count will move closer to y the next time around
while (count != y) {
rest of body
count += 1;
}
Completing the loop body
-
Our loop design tells us that count should be the number of times x has
been added.
-
each time we increment count, we should add x to the result again
while (count != y) {
result += x;
count += 1;
}
Initialization again
-
We have initialized count, and x and y are arguments to multiply method
-
What about result?
-
Loop design says that result == x * count.
-
If count is 0 (at beginning of loop), we have done no addition, so result
should be intialized to 0.
-
This makes sense also from our design which says that result = 0 + x +
x + ... + x
private int multiply (int x, int y) {
int count; // count == the number of additions done
int result; // result == 0 + x + x + ... + x, i.e., result == x * count
count = 0;
result = 0;
while (count != y) {
result += x;
count += 1;
}
return result;
}
Last modified: 3/29/99