And, Or, and Not: 3.5.1

  • use &&to make 2 things true when evaluating boolean expressions
    • Logical and
    • condition will only evaluate true if both are true
    • Often used to see if a number is in a range
      • (score => 0 && score <= 100)
  • use || if only one of two things has to be true
    • Logical or
    • This is inclusive
      • It will evaluate true if both things are true
    • Often used to check for error conditions with numerical values
      • (score < 0 || score > 100)
  • ! can be used to negate a boolean value
    • Logical not
    • Used in !=
    • Reverses a boolean value
      • !false is true
  • Order of operations is
    1. Parenthesis
    2. !
    3. &&
    4. ||

Truth Tables: 3.5.2

  • Expressions involving logical operators evaluate to a boolean value

P && Q

P Q P && Q
true true true
false true false
true false false
false false false

P || Q

P Q **P   Q**
true true true    
false true true    
true false true    
false false false    

Short Circuit Evaluation: 3.5.3

  • && and || use short circuit evaluation
    • The second expression isn’t always checked if the first is enough to evaluate an expression
    • expr1 || expr2
      • If expr1 is true, we don’t need to check expr2 to know the expression will be true
    • expr1 && expr2
      • If expr1 is false, we know the expression will be false regardless of what expr2 evaluates to

Truth Tables POGIL: 3.5.4

sunny raining temperature > 80 !raining && (sunny || temperature > 80)
false false false false
true false false true
true true false false
true true true false
true false true true
false false true true
false true false false

Summary: 3.5.5

  • Logical operators are used with boolean values
    • ! not
    • && and
    • || or
  • (A && B) is true if both A and B are
  • (A || B) is true if either A or B are
  • !A is true if A is false
  • Order of operations is ! -> && -> ||
  • When the result of a logical expression using && or || can be evaluated with the first expression, the second is ignored
    • Known as short circuit evaluation