Inferential Transformations
A Programming Pattern |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
boolean
Values:
if
)while
,
for
)boolean
Values:
>
, ==
)
evaluate to a boolean
value||
,
&&
) can be performed on
boolean
values and evaluate to a
boolean
valueboolean
expressions to reduce the mess
if
or else
blocks
if
statementsif
statements!
operator!(a > b)
becomes (a <= b)
!(a >= b)
becomes (a < b)
!(a < b)
becomes (a >= b)
!(a <= b)
becomes (a > b)
!(a == b)
becomes (a != b)
or
((a < b) || (a > b))
!(a && b)
becomes (!a) || (!b)
!(a || b)
becomes (!a) && (!b)
if
Statementsif
Statements (cont.)if
Statementsif
statements with identical
ramifications. For example:||
||
||
(cont.)||
is simpler and
clearer!if
Statements with a Twist:
sales[1]
doesn't exist if sales
has
a length of 1
boolean
expression when
the outcome can be determinedfalse && x
evaluates to false
(sometimes called the simplification rule)true || x
evaluates to true
(sometimes called the addition rule)