2022 CSCI1130-Tutorial03 Boolean and Operators
2022 CSCI1130-Tutorial03 Boolean and Operators
Using Java
布林 ( 布爾 )/ 真值 / 邏輯運算
A proposition may be true, false or undetermined,
e.g.
– true/false questions in examinations
– You are a boy
– Peter is 21 years old
– Principal of no less than HK$30,000
– It will rain tomorrow?
accountMichael.deposit(3000);
moneyAtHand = moneyAtHand - 3000;
noMoneyAtHand = true;
noMoneyAtHand = moneyAtHand <= 0;
moneyAtHand <=0 gives a True value
The only values a boolean type field can take is either true
or false.
3 Boolean & Rel Op CSCI1130, CS&E, The Chinese University of HK 3
Expressions
Examples:
int x = 15;
int y = 100;
System.out.println(x > y);
< 15);
System.out.println(x <= 15)
"Negate" y first,
== y);
then multiplication
System.out.println(x != 5);
* -y > 0);
System.out.println(x > y;
System.out.println(x
3 Boolean & Rel Op CSCI1130, CS&E, The Chinese University of HK 9
System.out.println(x
Floating point comparison
Why false?
– Numerical error
boolean iAmDry;
/*
* sunnyDay and noSweat iAmDry
*/
iAmDry = sunnyDay && noSweat;
--- 8< ---------------------------
What if it’s NOT sunny? [iAmDry false]
What if I DO sweat? [iAmDry false]
iAmDry noSweat
phoneRing callComing
a b a && b a || b !a a ^ b
true true true true false false
true false false true false true
false true false true true true
false false false false true false
Example:
Example:
boolean needPhone =
;
boolean areAllEqual = ;
Examples:
int x = 15; && is evaluated after relational
operators.
int y = 100; || is evaluated after &&.
System.out.println(x > y && x >= 15);
System.out.println(x < 15 || x > 15);
System.out.println(x == y && y == 100);
System.out.println(x != 5 && x < y);
System.out.println(x + y > 100 || y <= 10);
iAmDry noSweat
phoneRing callComing
or ( | ) true false
iAmDry noSweat
phoneRing callComing
or ( || ) true false
• de Morgan’s Law:
!(a && b) !a || !b
!(a || b) !a && !b
/* in expanded form: */
badMood = ! (sunnyDay && noSweat) ||
! (lowBattery && callForwarded);
a b a || b !(a || b) !a !b !a && !b
T T
T F
F T
F F
In Java:
!((age < 12) || (age >= 65))
In English:
Words neither rhyme nor alliterate.
In Java:
!wordsRhyme ___ && !
wordsAlliterate
Words don’t rhyme and they don’t alliterate
() Parentheses [highest]
< > == Relational/ Comparisons
! Negation
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
&& Logical AND
|| Logical OR [lowest]
3 Boolean & Rel Op CSCI1130, CS&E, The Chinese University of HK 38
More About Expression
isPrime = true;
int factorInTrial = (int) Math.sqrt(num);
while (factorInTrial >= 2)
{
// divisibility test with different factors
remainder = num % factorInTrial;
if (remainder == 0)
System.out.println("Found factor of " + num + ": " + factorInTrial);
isPrime = isPrime && (remainder != 0); // isPrime &&= remainder != 0;
factorInTrial--;
}
System.out.println("Input number " + num + " is prime? " + isPrime);
https://www.mpfa.org.hk/en/mpf-system/mandatory-contr
ibutions/employees#anchor1