Download presentation
Presentation is loading. Please wait.
1
CSS161: Fundamentals of Computing
Boolean Expressions This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing
2
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 ) CSS161: Fundamentals of Computing
3
Java Comparison Operators
CSS161: Fundamentals of Computing
4
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) 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 ) No Compilation error! It’s always true. CSS161: Fundamentals of Computing
5
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, thakn you. == in Strings compare their references. CSS161: Fundamentals of Computing
6
CSS161: Fundamentals of Computing
ASCII Code 32 56 8 80 P 104 h 33 ! 57 9 81 Q 105 iI 34 “ 58 : 82 R 106 j 35 # 59 ; 83 S 107 k 36 $ 60 < 84 T 108 l 37 % 61 = 85 U 109 m 38 & 62 > 86 V 110 n 39 ‘ 63 ? 87 W 111 o 40 ( 64 @ 88 X 112 p 41 ) 65 A 89 Y 113 q 42 * 66 B 90 Z 114 r 43 + 67 C 91 [ 115 s 44 , 68 D 92 \ 116 t 45 - 69 E 93 ] 117 u 46 . 70 F 94 ^ 118 v 47 / 71 G 95 _ 119 w 48 72 H 96 ` 120 x 49 1 73 I 97 a 121 y 50 2 74 J 98 b 122 z 51 3 75 K 99 c 123 { 52 4 76 L 100 d 124 | 53 5 77 M 101 e 125 } 54 6 78 N 102 f 126 ~ 55 7 79 O 103 g 127 CSS161: Fundamentals of Computing
7
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) CSS161: Fundamentals of Computing
8
CSS161: Fundamentals of Computing
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. CSS161: Fundamentals of Computing
9
CSS161: Fundamentals of Computing
Self-Test Exercises Work on Textbook P111’s exercises 13 ~ 16. CSS161: Fundamentals of Computing
10
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 CSS161: Fundamentals of Computing
11
CSS161: Fundamentals of Computing
Truth Tables CSS161: Fundamentals of Computing
12
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? CSS161: Fundamentals of Computing
13
CSS161: Fundamentals of Computing
Self-Test Exercises Work on Textbook p114’s exercises 17 ~ 18. CSS161: Fundamentals of Computing
14
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 ||. CSS161: Fundamentals of Computing
15
Precedence and Associativity Rules
CSS161: Fundamentals of Computing
16
CSS161: Fundamentals of Computing
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. CSS161: Fundamentals of Computing
17
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 CSS161: Fundamentals of Computing
18
CSS161: Fundamentals of Computing
Self-Test Exercises ( ( result = ( ++n ) + ( other = ( 2 * ( ++n ) ) ) ) What if n is 2 at first? ( ++n > 0 ) && ( 5 > n ) What if n is 0 at first? Work on Textbook p125’s exercises 19 ~ 21. CSS161: Fundamentals of Computing
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.