Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logical Operators & Truth Tables.

Similar presentations


Presentation on theme: "Logical Operators & Truth Tables."— Presentation transcript:

1 Logical Operators & Truth Tables

2 Logical Operators Boolean expressions can use the following logical operators: ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands) In addition to the equality and relational operators, Java has three logical operators that produce boolean results (true or false).

3 Logical NOT The logical NOT operation is also called logical negation or logical complement If some boolean condition a is true, then !a is false; if a is false, then !a is true Logical expressions can be shown using truth tables a !a true false The ! Operator is used to perform the logical NOT operator, which is also called the logical complement. The logical complement of a boolean value gives its oppose value. If a boolean variable called found has the value false, then !found is true. The logical NOT operation does not change the value stored in found. Because the logical NOT operator is unary, there are only two possible values for its one operand, true or false.

4 Logical AND and Logical OR
The logical AND expression a && b is true if both a and b are true, and false otherwise The logical OR expression a || b is true if a or b or both are true, and false otherwise (read after 3rd click) The && operator performs a logical AND operation. The result is true if both operands are true, but false otherwise. Since it is a binary operator and each operand has two possible values, there are four combinations to consider. (read after slide) The result of the logical OR operator is true if one of the other or both operands are true, but false otherwise. It is also a binary operator. The logical NOT has the highest precedence of the three logical operators, followed by logical AND, and then logical OR. Logical operators are often used as part of a condition for a selection or repetition statement.

5 Logical Operators Conditions can use logical operators to form complex expressions if (total < MAX+5 && !found) System.out.println ("Processing…"); Logical operators have precedence relationships among themselves and with other operators all logical operators have lower precedence than the relational or arithmetic operators logical NOT has higher precedence than logical AND and logical OR

6 Short Circuited Operators
The processing of logical AND and logical OR is “short-circuited” If the left operand is sufficient to determine the result, the right operand is not evaluated if (count != 0 && total/count > MAX) System.out.println ("Testing…"); Java uses short-circuit evaluations to speed up the evaluation of compound conditionals. As soon as the result of the final evaluation is know, then the result if returned and the remaining evaluations are not even performed. If count has a value of zero, it is not necessary to evaluate the rest of the expression since both operands have to be true to system.out.println “Testing” This type of processing must be used carefully

7 Truth Tables A truth table shows the possible true/false combinations of the terms Since && and || each have two operands, there are four possible combinations of conditions a and b a b a && b a || b true false

8 Truth Tables Specific expressions can be evaluated using truth tables
total < MAX found !found total < MAX && !found false true

9 Comparing Characters We can use the relational operators on character data The results are based on the Unicode character set The following condition is true because the character + comes before the character J in the Unicode character set: if ('+' < 'J') System.out.println ("+ is less than J"); We know what it means when we say that one number is less than another but what does it mean to say one character is less than another? Recall that characters in Java are based on the Unicode character set which orders all possible characters that can be used and therefore we can use the equality and relational operators on character data. The uppercase alphabet (A-Z) followed by the lowercase alphabet (a-z) appear in alphabetical order in the Unicode character set

10 Comparing Strings Remember that a character string in Java is an object We cannot use the relational operators to compare strings The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order The String class also contains a method called compareTo to determine if one string comes before another (based on the Unicode character set) The compareTo method is more flexible than the equals method (read slide first) The compareTo method returns a number rather than a Boolean value (true or false) like the equals method does. The return value is negative if the first string object is less than the second string object. The return value is cero if the two strings contain the same characters. The return value is positive if the firs string object is greater than the second string.

11 Lexicographic Ordering
Because comparing characters and strings is based on a character set, it is called a lexicographic ordering This is not strictly alphabetical when uppercase and lowercase characters are mixed For example, the string "Great" comes before the string "fantastic" because all of the uppercase letters come before all of the lowercase letters in Unicode Also, short strings come before longer strings with the same prefix (lexicographically) Therefore "book" comes before "bookcase"

12 Comparing Float Values
We also have to be careful when comparing two floating point values (float or double) for equality You should rarely use the equality operator (==) when comparing two floats In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal Therefore, to determine the equality of two floats, you may want to use the following technique: Another interesting situation occurs when floating point data is compared. You should rarely use the equality operator when comparing floating point values. Two floating pints values are equal according to the equality operator, only if all the binary digital sin their underlying representations match. If the compared values are the results of computation, they may not exactly equal. if (Math.abs(f1 - f2) < ) System.out.println ("Essentially equal.");


Download ppt "Logical Operators & Truth Tables."

Similar presentations


Ads by Google