Booleans in Java -
i beginner , started learn java language. encountered problem in book booleans. don't know how find value of questions , wondering if can give me sample of how code supposed like. appreciated.
question in book:
suppose value of b false , value of x 0. value of each of following expressions
a) b && x == 0
b) b || x == 0
c) !b && x == 0
d) !b || x == 0
e) b && x != 0
f) b || x != 0
g) !b && x != 0
h) !b || x != 0
i don't know how execute problem, help!
here how approach problem:
- recall
&&
,||
needboolean
on both sides - recall
!
inverts value ofboolean
- recall binary operations
!=
,==
convert pairs ofint
sboolean
s - start right side; decide if it's
true
orfalse
- put result line above in expression
- compute result using truth table corresponding logical operator.
let's use first couple of exercises examples:
b && x == 0
you know b
false
, result of and
known right away: it's false
- there's no need else.
b || x == 0
- you know
b
false
, result ofor
not known right away. - compute
x == 0
(the valuetrue
) - compute
b || true
(the valuetrue
, becauseor
-ingtrue
true
).
Comments
Post a Comment