Evaluating boolean Expressions } The simple numeric comparison operators and equality relations are easy to evaluate P Q P && Q } T o determine whether a more T T T complex logical formula is true or T F F false, we use truth tables for logical operators F T F F F F P ! P Class #12: P Q P || Q Selection, Logic, and Control, II T F T T T F T T F T Software Design I (CS 120): D. Mathias F T T F F F 2 Software Design I (CS 120) Writing boolean Expressions Evaluating boolean Expressions } The logical operators allow us to write more complex if - } Using the basic truth tables, and our precedence rules, we conditions, to check more complex properties can evaluate any complex logical expression we like… } E.g., we can check if integer x is between 1 and 10 (inclusive): if ( ( x >= 1 ) && ( x <= 10 ) ) { System.out.println( x + “ is between 1 and 10” ); P Q ! P && Q } T T F F } Sometimes this is just for convenience: for example, both of the following pieces of code do the same thing : T F F F if ( x <= 0 ) { F T T T System.out.println( x + “ is <= than 0” ); F F T F } if ( !( x > 0 ) ) { Step 1: evaluate the Step 2: evaluate the System.out.println( x + “ is <= than 0” ); highest-priority lower-priority operator operator (! == NOT) (&& == AND) } 3 4 Software Design I (CS 120) Software Design I (CS 120) 1
Evaluating boolean Expressions Some Legal and Illegal Expressions } For example, suppose we have variables: int i1 = 3; int i2 = 4; x == -3, y == 0 char c = ‘e’; } Then, we can evaluate the following: if ( i1 != i2 ) { if ((c == ‘a’) || (c == ‘e’)) { ... ... ( x <= y ) && !( y > 10 || y == 1 ) true true } } T F F Step 1: evaluate basic if (!(i1 != i2)) { if ( i1 < = i2 ) { Step 2: relations ... ... (parentheses false ERROR F used to set } } Spacing matters! order) T if ((0 < i1) && (i2 >= 4)) { if ( 0 <= i1 <= 5 ) { Step 3: evaluate ... ... ERROR Step 4: return the operations in true } } T final value of the precedence order All these operators are expression binary (only 2 arguments) 5 6 Software Design I (CS 120) Software Design I (CS 120) Some Exercises Short-Cut Evaluation } Suppose num1 and num2 are integer variables: } Programmers are always trying to make programs run faster, by never doing more work than is needed Write code that prints out the sum of the variables if they 1. are both less than 7 . } For example, the creators of Java wanted to make sure that we could get the final output value of a complicated boolean expression as fast as possible Write code that prints out the sum of the variables if num1 2. } Therefore, when a program executes, it will use some is less than 7 , but num2 is not. short-cuts in evaluating these expressions: If either side of a conjunction ( && ) is false , the entire 1. Write code that prints out the largest of the two variables; 3. expression is also false . We can stop . if they are equal, it should print out the word EQUAL . If either side of a disjunction ( || ) is true , the entire 2. expression is also true. We can stop . 7 8 Software Design I (CS 120) Software Design I (CS 120) 2
Short-Cut Evaluation Using Short-Cut Evaluation } Because of the short-cuts, things that are logically equivalent in do not } We can sometimes use this feature to our advantage always do the same thing in a program } As an example, we can order our instructions so that the first one that is } In logical terms, e.g., these two things always mean exactly the same: false causes the evaluation of the boolean condition to stop immediately } This allows us to write code that is more compact and less buggy: if ( X && Y ){ if ( Y && X ){ ... } ... } String data = input.getText(); If given input of length } But in Java execution, there is a difference—since we go left-to-right in less than 6, this crashes if ( data.charAt( 5 ) == ‘a’ && data.length() > 5 ) { evaluating the expression, they each work differently: since it can’t get character System.out.println( “One thing” ); at position 5 at all. } if ( X && Y ){ if ( Y && X ){ ... } ... } If given input of length String data = input.getText(); less than 6, this works First , check X . First , check Y . if ( data.length() > 5 && data.charAt( 5 ) == ‘a’ ) { since it sees that it is too If false , stop and skip . If false , stop and skip . short, and skips looking System.out.println( “Another thing” ); for the character that } If X is true , If Y is true , doesn’t actually exist. then check Y . then check X . 9 10 Software Design I (CS 120) Software Design I (CS 120) 3
Recommend
More recommend