Java Reference
In-Depth Information
values might have been reversed, in which case
y
will be larger than
x
. So, that asser-
tion is sometimes true and sometimes false at point F.
What about the assertion
x == y
? We know it doesn't have to be true because we
have seen cases in which
x
is greater than
y
or
y
is greater than
x
. Is it possible for it
to be true? Are there any values of
x
and
y
that would lead to this outcome? Consider
the case in which
x
is
218
and
y
is
21
. Then we would divide
x
by 10 to get
21
,
which would equal
y
. So, this assertion also is sometimes true and sometimes false.
There was no change to
z
between points D and E and point F, so we simply carry
our answer down from the previous columns. So we would fill in the table as follows
for point F:
x > y
x == y
z == 0
sometimes
sometimes
never
Point F
Point G appears after the
while
loop:
while (x != y) {
...
}
// Point G
We can escape the
while
loop only if
x
becomes equal to
y
. So, at point G we know
that the assertion
x == y
is always true. That means that the assertion
x > y
can never
be true. The assertion
z == 0
is a little tricky. At point F it was never true, so you
might imagine that at point G it can never be true. But we weren't necessarily at point F
just before we reached point G. We might never have entered the
while
loop at all, in
which case we would have been at point A just before point G. At point A the variable
z
was equal to
0
. Therefore, the right answer for this assertion is that it is sometimes true,
sometimes false at point G. The final row of our table thus looks like this:
x > y
x == y
z == 0
Point G
never
always
sometimes
When we combine this information, we can fill in our table as follows:
x > y
x == y
z == 0
Point A
sometimes
sometimes
always
Point B
sometimes
never
sometimes
Point C
sometimes
never
never
Point D
always
never
never
Point E
never
never
never
Point F
sometimes
sometimes
never
Point G
never
always
sometimes
Search WWH ::
Custom Search