Java Reference
In-Depth Information
for the assertions x > y and x == y . The assertion z == 0 will never be true after
the increment, even though z starts at 0 before the loop begins and there are no other
manipulations of the variable inside the loop; once it is incremented, it will never be
0 again. Therefore, we can fill in the table for point C as follows:
x > y
x == y
z == 0
sometimes
never
never
Point C
Points D and E are part of the if/else statement inside the while loop, so we
can evaluate them as a pair. The if/else statement appears right after point C:
// Point C
if (x > y) {
// Point D
x = x / 10;
} else {
// Point E
y = y / 10;
}
No variables are changed between point C and points D and E. Java performs a test
and branches in one of two directions. The if/else test determines whether x is greater
than y . If the test is true , we go to point D. If not, we go to point E. So, for the assertion
x > y , we know it is always true at point D and never true at point E. The assertion x
== y is a little more difficult to work out. We know it can never be true at point D, but
could it be true at point E? Solely on the basis of the if/else test, the answer is yes.
But remember that at point C the assertion could never be true. The values of x and y
have not changed between point C and point E, so it still can never be true.
As for the assertion z == 0 , the variable z hasn't changed between point C and
points D and E, and z is not included in the test. So whatever we knew about z before
still holds. Therefore, the right answers to fill in for points D and E are as follows:
x > y
x == y
z == 0
always
never
never
Point D
Point E
never
never
never
Point F appears after the if/else statement. To determine the relationship
between x and y at point F, we have to look at how the variables have changed. The
if/else statement either divides x by 10 (if it is the larger value) or divides y by 10
(if it is the larger value). So, we have to ask whether it is possible for the assertion x
> y to be true at point F. The answer is yes. For example, x might have been 218 and
y might have been 6 before the if/else statement. In that case, x would now be 21 ,
which is still larger than y . But does it have to be larger than y ? Not necessarily. The
 
Search WWH ::




Custom Search