170 likes | 372 Views
Boolean Expressions. This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials. Simple Boolean Expressions. A Boolean expression is an expression that returns either true or false .
E N D
Boolean Expressions This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials. SWE 510: Object Oriented Programming in Java
Simple Boolean Expressions • A Boolean expression is an expression that returns either true or false. • The simplest expressions are comparisons of two expressions: time < limit balance <= 0 • If a Boolean expression is used in an if-else statement, it must be enclosed in parentheses. if ( time < limit ) SWE 510: Object Oriented Programming in Java
Java Comparison Operators SWE 510: Object Oriented Programming in Java
PITFALL 1: Using = in Place of == • Comparison of two integers • Correct int yourScore, myScore; ...; // yourScore, myScore initialized if ( yourScore == myScore ) System.out.println( “A tie” ); • Incorrect if ( yourSocre = myScore) System.out.println( “A tie” ); Compilation error: incompatible types found : int required: boolean if ( yourScore = myScore ) ^ • Comparison of two booleans • Correct boolean passedCSS161 = false; ...; // passedCSS161 modified if ( passedCSS161 == true ) System.out.println( “passed” ); • Incorrect if ( passedCSS161 = true ) System.out.println( “passed” ); No Compilation error! It’s always true. SWE 510: Object Oriented Programming in Java
PITFALL 2: Using == with Strings • Incorrect String greetings; Scanner keyboard = new Scanner( System.in ); greetings = keyboard.nextLine( ); if ( greetings = “How are you?” ) System.out.println( “Fine, thank you.” ) Output: no outputs • Correct if ( greetings.equals( “How are you?” ) ) System.out.println( “Fine, thank you.” ); Output: Fine, thank you. • == in Strings compare their references. SWE 510: Object Oriented Programming in Java
ASCII Code SWE 510: Object Oriented Programming in Java
Alphabetical and Lexicographic Order • Alphabetical Order: A(a), B(b), C(c), …, Z(z) • Lexicographic Order: based on ASCII CODE • String has three methods: • string1.equals( string2 ) • Returns true if string1 matches string2 exactly, otherwise false. • string1.compareTo( string2 ) • Based on lexicographic order • Returns 0 if string1 matches string2 exactly, a positive integer if string1 comes before string2, otherwise a negative integer • string1.compareToIgnore( string2 ) • Based on alphabetical order (in uppercase letters) SWE 510: Object Oriented Programming in Java
Example String string1 = “abcd”; String string2 = “abaf”; System.out.println( string1.compareTo( string2 ); // ‘a’ – ‘a’ = 0; ‘b’ – ‘b’ = 0; ‘c’ – ‘d’ = 2; thus return 2. // Note that ‘d’ – ‘f’ = -2 but this comparison is ignored. String string3 = “abcd”; String string4 = “ABCD”; System.out.println( string3.compareTo( string4 ); // ‘a’- ‘A’ = -32; thus return -32. System.out.println( string3.compareToIgnore( string4 ); // All letters are compared in uppercase. Thus return 0. SWE 510: Object Oriented Programming in Java
Building Boolean Expressions • Two Boolean operators to combine Boolean expressions: • AND ( expression1 && expression2 ): returns true if both expressions are true. ( number > 2 ) && ( number < 7 ) // will be true if number is 3, 4, 5, 6 • OR ( expression1 || expression2 ): returns true if either one of them is true. ( count < 3 ) || ( count > 12 ) // will be true if count is …, -2, -1, 0, 1, 2, 13, 14, 15, 16, … • Negation( ! ) used to negate a given expression: • !expression1: returns true if it is false. ! (savings < debt ) // true if savings >= debt SWE 510: Object Oriented Programming in Java
Truth Tables SWE 510: Object Oriented Programming in Java
Evaluating Boolean Expressions • !( ( count < 3 ) || ( count > 7 ) ) • What if count is 8? • ( number > 0 ) && !( number % 2 == 0 ) && !( number % 3 == ) • What numbers make this expression true? • !( data < 0 ) && ( ( data / 100 < 1 ) || ( data % 100 == 0 ) ) • What data make this expression true? SWE 510: Object Oriented Programming in Java
Short-Circuit or Lazy Evaluation • expression1 && expression2 && expression3 • False && false && false returns false • False && false && true returns false • False && true && true returns false • Thus, if expression1 is false, no evaluation on expressions 2 and 3. • expression1 || expression2 || expression3 • True || true || true returns true • True || true || false returns true • True || false || false returns true • Thus, if expression1 is true, no evaluation on expressions 2 and 3. • Useful to prevent a runtime error if ( ( kids != 0 ) && ( ( pieces / kids ) >= 2 ) ) System.out.println( “Each child may have two pieces!” ); • Complete evaluation • Use & and | instead of && and ||. SWE 510: Object Oriented Programming in Java
Precedence and Associativity Rules SWE 510: Object Oriented Programming in Java
Questions • Include parentheses in each of the following expressions to keep their operator precedence. • bonus + balance * rate / correctionFactor – penalty • number1 = number2 = number3 + 7 * factor • number + 1 > 2 || number + 5 < -3 • expenses < income || expenses < savings || creditRating > 0 • Recommended parentheses rule: include most parentheses, except where the intended meaning is obvious. SWE 510: Object Oriented Programming in Java
Side Effects and Evaluation Rule • Side Effects: when, in addition to returning a value, an expression changes something, such as the value of a variable • The assignment, increment, and decrement operators all produce side effects • Evaluation Rule: • Perform binding: • Determine the equivalent fully parenthesized expression using the precedence and associativity rules • Proceeding left to right, evaluate whatever subexpressions can be immediately evaluated • These subexpressions will be operands or method arguments, e.g., numeric constants or variables • Evaluate each outer operation and method invocation as soon as all of its operands (i.e., arguments) have been evaluated SWE 510: Object Oriented Programming in Java