Upload
ekram
View
27
Download
0
Embed Size (px)
DESCRIPTION
5-4 Boolean Operators : Every Comparison yields a boolean (true or false) outcome. Machine Code (Binary) is based on whether or not a state is true(1) or false(0). Boolean Operators &&“and”T && T -> T Both side must be true! || “or”T || F -> TAt least one side must be true! - PowerPoint PPT Presentation
Citation preview
5-4 Boolean Operators : Every Comparison yields a boolean (true or false) outcome. Machine Code (Binary) is based on whether or not a state is true(1) or false(0). Boolean Operators
&& “and” T && T -> T Both side must be true! || “or” T || F -> T At least one side must be true! ! “negate” !T -> F Negate or switch outcome
Consider: if( x > 3 && x <= 10) // 3 < x < = 10 if( x < 0 || x >= 5) if( !(x >= 0)) // Same as? if(inRange(num)) // Where inRange(int) declared:
public boolean inRange(int num ){
if( num > 3 && num <= 10) // 1.return true;
elsereturn false;
} //or 2. return (num > 3 && num <= 10);
//or 3. if(num > 3 ) if( num <= 10)
return true; return false;
DeMorgan’s Law !(A && B) = !A || !B !(A || B) = !A && !B
Examples: !( x > 0 && y <= 0) !( x != 0 || y == 0) !( x%2 != 0 || ( x%10 == 0 && x % 5 = = 0))
What does the following print?x = 3;y = x+4;if( x >= y/4 && x != y%2)
y = 2*x - y;else
x = x-y;if( !(x < y) )
x = y -3*x;else
y = x;
System.out.print(“x = “ +x + “ y = “ +y);