Boolean is a value that is either true or false - for example, a boolean variable like rainy might contain either true or false.
An expression is a collection of values and operations that produce a value. A boolean expression is an expression that produces either true or false as a result.
Boolean expressions appear in if(....) commands - inside the parentheses. This is usually simple, but using and (&&) operators and or (||) operators produces complex expressions.
Assuming that a=4 , b=6 and c=8 , decide the value of each boolean expression below.
int a=4 , b=6 , c=8;
boolean
pythagoras = ( a*a + b*b == c*c ); // true or
false?
boolean digits = ( a<10
&& b<10 &&
c<10);
boolean bad = (2*b - c+a ==
0);
boolean good = ( a%2 == 0 || b%4
== 0 || c%8 == 0 )
;
boolean
inOrder = ( a < b && b < c
&& c < (a+b) );
pythagoras == false 16 + 36 = 52, but 8*8 = 64, so not equal
digits == true yes, all three numbers are below 10
bad == false 2*6 - 4 + 8 = 12 - 4 + 8 = 16, not zero
good == true true || false || true = true, only need 1 true
inOrder == true 4 < 6 and 6 < 8 and 8 < 10, all true