ICS Boolean Logical Operators What is a Boolean Expression? A Boolean expression is an expression that evaluates to TRUE or FALSE. Examples in English: “It is not snowing” TRUE FALSE “I am human” TRUE FALSE “I am human and I have 12 fingers.” TRUE FALSE Examples in C#: x != 57 TRUE when ______________________________________________ x == 28 TRUE when ______________________________________________ Review: The basic comparison operators: == means ______________________ != means ______________________ >= means ______________________ <= means ______________________ < means ______________________ > means ______________________ The && Operator (AND) The && (and) operator returns TRUE when both the things on either side of it are true, and false otherwise… I am human I am a dog I am human && && && I have 10 fingers I have 12 toes I have 12 toes TRUE FALSE FALSE (both true) (both false) (one of them is false) When will these be true? x == 4 && y < 7 ________________________________________ a >= 5 && b <= 4 && name == “bob” ________________________________________ x == 4 && x == 2 ________________________________________ ICS Boolean Logical Operators The || Operator (OR) The | | (or) operator returns TRUE when one or both of the things on either side of it are true, and false otherwise. You find this symbol to the right of the curly brackets on most keyboards. I am human I am a dog I am human || || || I have 10 fingers I have 12 toes I have 12 toes TRUE FALSE TRUE (both true) (both false) (one of them is true) When will these be true? x == 4 || y < 7 _________________________________________ a >= 5 || b <= 4 || s.Equals("bob") _________________________________________ x == 4 || x == 2 _________________________________________ NOTE: Logical OR is different from regular English OR Usually if I say I’m going to bed or watching TV, we interpret that to mean I will do one or the other but not both. But to a computer, the statement that I am going to bed or watching TV could also mean that I am going to go to bed AND watch TV… A Common Mistake The following will not work … if (x < 5 && > 1) … if (x == 5 || 6) … This will give you a syntax error, even though it is clear to us humans what it is supposed to mean. The problem is that the & and | operators need to have fully formed Boolean expressions to either side of them. This is the correct way to write it… if (x < 5 && x > 1) … if (x == 5 || x == 6) Evaluate the following expressions Say whether each evaluates to TRUE or FALSE 1. 1+2 > 4-2 && 12 < 23 ___ 2. 1+2 > 4-2 || 12 < 23 ___ 3. 1+2 > 4-2 && 12 > 23 ___ 4. 1+2 > 4-2 || 12 > 23 ___ ICS Boolean Logical Operators The ! Operator (NOT) The ! (not) operator returns TRUE when the thing beside it is FALSE, and FALSE when the thing beside it is TRUE. !(I am human) !(I am a dog) !(I am human || I have 12 toes) FALSE TRUE FALSE When will these be true? !(age == 2) _________________________________________ a >= 5 || !(b <= 4) _________________________________________ !(x == 4 || x == 2) _________________________________________ Evaluate the Following mood>=3 && mood<8 TRUE for which values of mood? 1 2 3 4 5 6 7 8 9 10 11 mood == 5 || mood > 10 TRUE for which values of mood? 1 2 3 4 5 6 7 8 9 10 11 !(mood>=3 && mood<8) TRUE for which values of mood? 1 2 3 4 5 6 7 8 9 10 11 !(mood == 5) || mood > 10 TRUE for which values of mood? 1 2 3 4 5 6 7 8 9 10 11 Order of Operations for the Boolean Operators Just like arithmetic expressions, Boolean expressions have an order of operations. The order is… 1. Brackets 3. Arithmetic Operators (+, -, / *, %) 2. Boolean Operators (==, !=, <, etc.) 3. Not 4. And 5. Or Instead of BEDMAS, it’s BABNAO! ICS Boolean Logical Operators Evaluate the Following For each of these expressions, state whether it does what it is supposed to do. If it’s wrong, give an example where it fails and show how to fix it. An expression that’s true if both x and y are equal to either 8 or 9. x == 8 || x == 9 && y == 8 || y == 9 An expression that’s true when x is in the range 0 to 5 or 10 to 15 inclusive. x > 0 && x < 5 || x > 10 && x < 15 What will this code print in the Message Box? int x = 5, y = -4; double z = 7.8; if (x == 5) MessageBox.Show("duck"); else MessageBox.Show ("goose"); if (x !=5 && z < 8) MessageBox.Show ("duck"); else MessageBox.Show ("goose"); OUTPUT: if (y > 0 || z < 8 ) MessageBox.Show ("goose"); else MessageBox.Show ("duck"); ____________ ____________ ____________ Write the following expressions. 1. This expression is TRUE if the variable mark is in the range 70 to 80. This range includes 70 but does not include 80. 2. This expression is TRUE if the variable userInput is 5 or 0. 3. This expression is TRUE if the variable number is greater than 0 but not equal to 3. 4. This expression is TRUE if the variable age is 16, 18, 19, or 25. 5. This expression is TRUE if a string is "Yes".