Presentation is loading. Please wait.

Presentation is loading. Please wait.

7-1 boolean Data Type George Boole (1815 - 1864) boolean variables may have only two values, true or false You define boolean fields or boolean local variables.

Similar presentations


Presentation on theme: "7-1 boolean Data Type George Boole (1815 - 1864) boolean variables may have only two values, true or false You define boolean fields or boolean local variables."— Presentation transcript:

1 7-1 boolean Data Type George Boole (1815 - 1864) boolean variables may have only two values, true or false You define boolean fields or boolean local variables the same way as other variables boolean true false Reserved words private boolean hasMiddleName; boolean isRolling = false;

2 7-2 Boolean Expressions if ( ) is a Boolean expression A Boolean expression evaluates to either true or false Boolean expressions are written using boolean variables and relational and logical operators

3 7-3 Relational Operators, =, ==, != is equal to is NOT equal to

4 7-4 Relational Operators (cont’d) Apply to numbers or chars: if ( count1 <= count2 )... if ( sum != 0 )... if ( letter == 'Y' )... Do not use == or != with doubles because they may have rounding errors double x = 7.0; double y = 3.5; if (x / y == 2.0)... May be false!

5 7-5 Relational Operators (cont’d) Be careful using == and != with objects (for example, Strings): they compare references (addresses) rather than values (the contents) String cmd = scan.nextLine (); if ( cmd == "Help" )... Wrong! (always false)

6 7-6 Relational Operators (cont’d) Use the equals or equalsIgnoreCase methods to compare Strings: or if ( "Help".equals (cmd) )... String cmd = scan.nextLine(); if ( cmd.equals ("Help") )...

7 7-7 Relational Operators (cont’d) Use the == or != operator with strings and other objects when you want to know whether or not this is exactly the same object Also use == or != to compare to null String text = file.nextLine( ); if ( text != null )...

8 7-8 Logical Operators &&, ||, ! and or not

9 Equivalent Boolean Expressions !(X>Y) and X<=Y are equivalent Example: !(speed > 2000) can be re-written as (speed <= 2000)

10 Equivalent Relational Expressions Usually if you have an expression that uses a NOT operator, replace it with an equivalent comparison that does not use a NOT. ExpressionEquivalent ExpressionEquivalent !(X < Y)X >= Y !(X >= Y)X < Y !(X > Y)X <= Y !(X <= Y)X > Y !(X == Y)X != Y !(X != Y)X == Y

11 11 Compound Boolean Expressions Simple Expressions Have one comparison (hours > 40) Compound Expressions Have more than one comparison Created by using And (&&) and Or (||) operators (qty > 0 && qty < 51) (qty 51)

12 7-12 Compound Boolean Expressions ( condition1 && condition2 ) is true if and only if both condition1 and condition2 are true ( condition1 || condition2 ) is true if and only if condition1 or condition2 (or both) are true !condition1 is true if and only if condition1 is false

13 7-13 Compound Boolean Expressions &&, ||, and ! obey the laws of formal logic called De Morgan’s Laws: Example: if ( ! ( x => - 10 && x <= 10 ) )... if ( x 10 )... ! (p && q) == ( !p || !q ) ! (p || q) ==( !p && !q ) Easier to read

14 7-14 Operator Precedence ! - (unary) ++ -- ( cast ) * / % + - >= == != && || if ( ( ( year % 4 ) == 0 ) && ( month == 2 ) )... if ( year % 4 == 0 && month == 2 )... Easier to read Highest Lowest

15 Operator Precedence Put parentheses in this boolean expression so that it correctly tests applicants: if ( college >= 4 && experience >= 2 || gpa > 3.5 ) System.out.println("Interview applicant"); else System.out.println("Send resume to circular file."); 1 st. Arithmetic operators 2 nd. Relational operators 3 rd. Logical operators (&& before ||) 1 st. Arithmetic operators 2 nd. Relational operators 3 rd. Logical operators (&& before ||)

16 7-16 Short-Circuit Evaluation if (condition1 && condition2)... If condition1 is false, then condition2 is not evaluated (the result is false anyway) if (condition1 || condition2)... If condition1 is true, then condition2 is not evaluated (the result is true anyway) if ( x >= 0 && Math.sqrt (x) < 15.0)... Always OK: won’t get to sqrt if x < 0

17 Cascading && Operators When there are more than one && operator in an expression, pairs of operands are grouped together with an && starting from the left and going toward the right of the expression. Each group works as a single operand for the next && to the right. Assume that V, W, X, Y, and Z stand for boolean operands. Then operands are grouped from left to right: V && W && X && Y && Z (V && W) && X && Y && Z ( (V && W) && X ) && Y && Z ( ( (V && W) && X ) && Y) && Z

18 Cascading || Operators The || operator also has left to right associativity. When an expression has several ||'s look for a true starting from the left and going toward the right. The first true stops the evaluation and causes the entire expression to be true. If every operand is false then every operand is evaluated and the entire expression is false.

19 For example, say that A, B, C, and D stand for relational expressions (things like 23 > 90). Then, Logical Operator Precedence A || B && CmeansA || (B && C) A && B || C && Dmeans(A && B) || (C && D) A && B && C || Dmeans( (A && B) && C ) || D !A && B || Cmeans( (!A) && B ) || C


Download ppt "7-1 boolean Data Type George Boole (1815 - 1864) boolean variables may have only two values, true or false You define boolean fields or boolean local variables."

Similar presentations


Ads by Google