Java Reference
In-Depth Information
!(A || B) is the same as !A && !B
Pay particular attention to the fact that the and and or operators are reversed by
moving the not inwards. For example, the negation of Ȓthe input is S or the input
is Mȓ,
! (input.equals("S") || input.equals("M"))
is Ȓthe input is not S and the input is not Mȓ
!input.equals("S") && !input.equals("M")
Let us apply the law to the negation of ȓthe amount is between 0 and 1000ȓ:
! (0 < amount && amount < 1000)
is equivalent to
!(0 < amount) || !(amount < 1000)
which can be further simplified to
0 >= amount || amount >= 1000
Note that the opposite of < is >= , not >!
209
210
5.4.4 Using Boolean Variables
You can use a Boolean variable if you know that there are only two possible values.
Have another look at the tax program in Section 5.3.2 . The marital status is either
single or married. Instead of using an integer, you can use a variable of type
boolean :
You can store the outcome of a condition in a Boolean variable.
private boolean married;
The advantage is that you can't accidentally store a third value in the variable.
Then you can use the Boolean variable in a test:
if (married)
. . .
Search WWH ::




Custom Search