Java Reference
In-Depth Information
Properties of boolean operators
Sometimes, we want to manipulate a boolean expression to put it in anoth-
er form. For example, we can replace an expression
Lesson page 6-
6 discusses
boolean proper-
ties.
!(x <= y || y <= z)
by the simpler expression
x>y && y>z
Manipulating boolean expressions in this fashion requires knowing the
properties of the operators. Many of these properties are similar to those of arith-
metic operators. For example, || , like + , is associative: x||(y||z)=(x||y)
|| z .
The marks of a Boolean tyro
Suppose a program uses a boolean variable atHome and this variable is to be
tested in an if-condition. It is common for tyros to use an if-statement with the
condition
Activity
6-6.4
atHome == true
You may be wondering what a tyro is. It has nothing to do with the word
tyrant . Also, it is not a greek fast food delicacy, a sloppy beef-lamb thing
wrapped in pita bread; that is a gyro . A tyro is a neophyte, a person who is famil-
iar with the rudiments of a subject but lacking in practical experience.
But back to programming in Java. The more experienced programmers real-
ize that b== true is equivalent to b . That is one of the simple properties of oper-
ator equivalence. So they write the if-statement with a much simpler condition:
atHome
Similarly, the tyro will use the expression atHome == false , while the expe-
rienced programmer will use the simpler !atHome .
Another mark of the tyro is this if-statement:
if (atHome || atWork)
b= true ;
else b= false ;
The pro would instead write a single assignment statement whose righthand side
is the same as the condition of the if-statement:
b= atHome || atWork;
There is nothing wrong with being a tyro in the field of programming. After
all, experienced programmers were once tyros, too. But tyros often do not want
others to know that they are tyros. If you do not, stay away from the marks of a
boolean tyro.
Search WWH ::




Custom Search