Java Reference
In-Depth Information
(2) mean the same thing.
Since (1) and (2) mean the same thing whether or not Bill has black hair,
they mean the same thing.
It takes time to get used to writing relations using form (2) instead of (1). Do
the self-help exercises at the end of this section to make the transition easier.
Examples
Here are more examples in which using the value of a relation provides a
better alternative. This statement stores the value of relation x<y in variable v :
if (x < y) { b= true ; }
else { b= false ; }
Instead of this if-statement, you can use this assignment:
b= x < y;
Now consider this specification of a function:
Return true if x is less than y and false otherwise.
Instead, write more simply:
Return x < y .
Here, the value of the relation x<y , which is written as a Java boolean expres-
sion, is to be returned.
One goal of the earlier part of this discussion is to make clear to you that
English is ambiguous. Computer programs should never be ambiguous: when
writing a specification of part of a program, be alert to any possible ambiguities
and do your best to remove them. Your teammates will love you for it.
A footnote on
lesson page 1-6
has a hilarious
example of
ambiguity.
2.6.2
Assertions
Below, the assignment is preceded and followed by a comment:
Activity
1-6.3
// {x > 0}
x= x + 1;
// {x > 1}
Each comment is a relation enclosed in braces { and } . We call such a relation
an assertion because we are asserting something about the state of execution
whenever the place where the comment appears is reached. Here is how to read
this code:
The code says nothing about what the statement does if x≤0 ! It
deals only with x>0 . And, if x is greater than 0 initially, when the
statement terminates, x will be greater then 1 .
Search WWH ::




Custom Search