3.5 Boolean Expressions

23 阅读1分钟

1. Exam Points

  • Logical operators NOT, AND, and OR.
    • For AND, true and true evaluates to true, otherwise false.
    • For OR, false and false evaluates to false, otherwise true.
    • For AND, if the first condition is false, no need to evaluate the second condition.
    • For OR, if the first condition is true, no need to evaluate the second condition.
  • Relational operators: =, ≠, >, <, ≥, and ≤
  • Equivalent expressions:
    • NOT (input1 OR input2) is equivalent to (NOT input1) AND (NOT input2)
    • NOT (input1 AND input2) is equivalent to (NOT input1) OR (NOT input2)
    • Work on logic gates: AND gate and OR gate.

2. Knowledge Points

(1) Boolean Expressions

  • The way statements are sequenced and combined in a program determines the computed result.
  • Programs incorporate iteration and selection constructs to represent repetition and make decisions to handle varied input values.
  • A Boolean value is either true or false.
    • Example:
      • 4 > 3 evaluates to true
      • 15 MOD 2 = 0 evaluates to false
  • An expression that evaluates to a Boolean value is a Boolean expression.
    • Example:
      • 10 > 2 is a Boolean expression
      • 10 > 2 AND 3 > 4 is a Boolean expression
      • 10 + 2 is not a Boolean expression

(2) Relational Operators

  • Relational operators are used in Boolean expressions.
    • =, ≠, >, <, ≥, and ≤
    • Example:
      • a = b
      • a ≠ b
      • a > b
      • a < b
      • a ≥ b
      • a ≤ b

(3) Logical Operators

  • Logical operators NOT, AND, and OR, which are also used in Boolean expressions.
    • AND : evaluates to true if both conditions are true, otherwise it evaluates to false
      image.png
    • OR : evaluates to false if both conditions are false, otherwise it evaluates to true
      image.png
    • NOT : evaluates to true if condition is false, otherwise it evaluates to false
      image.png
  • Examples:
    • (4 > 3) AND (4 < 6)
    • (4 > 3) AND (5 MOD 2 = 0)
    • (4 < 3) OR (4 < 6)
    • (5 MOD 2 = 0) OR (5 MOD 3 = 1)
    • NOT (4 > 3)
    • NOT (a > b)
  • Equivalent expressions:
    • NOT (input1 OR input2) is equivalent to (NOT input1) AND (NOT input2)
    • NOT (input1 AND input2) is equivalent to (NOT input1) OR (NOT input2)

3. Exercises