.. _compound-boolean-expressions: Compound Boolean Expressions ============================ Just before we looked at the If … Then statement we looked at Boolean expressions. Boolean expressions have two and only two potential answers, either they are true or false. So far we have looked at just simple Boolean expression, with just one comparison. A Boolean expression can actually have more than one comparison and be quite complex. A compound Boolean expression is generated by combining more than one simple Boolean expression together with a `logical operator `_ NOT, AND & OR. NOT is used to form an expression that evaluates to True only when the operand is false. AND is used to form an expression that evaluates to True only when both operands are true. OR is used to form an expression that evaluates to true when either operand is true. Here is a truth table for each operator: NOT Truth Table +-------+-------+ | A | NOT A | +=======+=======+ | True | False | +-------+-------+ | False | True | +-------+-------+ AND Truth Table +-------+-------+---------+ | A | B | A AND B | +=======+=======+=========+ | True | True | True | +-------+-------+---------+ | True | False | False | +-------+-------+---------+ | False | True | False | +-------+-------+---------+ | False | False | False | +-------+-------+---------+ OR Truth Table +-------+-------+---------+ | A | B | A OR B | +=======+=======+=========+ | True | True | True | +-------+-------+---------+ | True | False | True | +-------+-------+---------+ | False | True | True | +-------+-------+---------+ | False | False | False | +-------+-------+---------+ The general form in a programming language for a compound Boolean expression is: NOT ^^^ | **IF** not(Boolean expression #1) **THEN** | Statements to be performed ... | **ELSE** | Statements to be performed ... | **ENDIF** AND ^^^ | **IF** ((Boolean expression #1) and (Boolean expression #2)) **THEN** | Statements to be performed | **ELSE** | Statements to be performed ... | **ENDIF** OR ^^ | **IF** ((Boolean expression #1) or (Boolean expression #2)) **THEN** | Statements to be performed | **ELSE** | Statements to be performed ... | **ENDIF** In some programming languages the operators are simply the words not, and & or. In others they are “!” for NOT, “&&” for AND & “||” for OR. In this example program, the user is asked to enter a term mark and a final project mark. The program then tells the user if they passed the course or not. The rule for passing the course is that the student must have a term mark of at least 50% **and** a final project mark of at least 50. The program uses a compound Boolean expression to determine if the student passed the course. Top-Down Design for Compound Boolean Expression statement ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. image:: ./images/top-down-compound-boolean.png :alt: Top-Down Design for Compound Boolean Expression statement :align: center Flowchart for Compound Boolean Expression statement ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. image:: ./images/flowchart-compound-boolean.png :alt: Compound Boolean Expression flowchart :align: center Pseudocode for Compound Boolean Expression statement ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | **GET** term_mark | **GET** project_mark | **IF** ((term_mark >= 50) and (project_mark >= 50)) **THEN** | **SHOW** "You passed the course." | **ELSE** | **SHOW** "You failed the course." | **ENDIF** Code for Compound Boolean Expression statement ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. tabs:: .. group-tab:: C .. literalinclude:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/C/main.c :language: C :linenos: :emphasize-lines: 22-26 .. group-tab:: C++ .. literalinclude:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/CPP/main.cpp :language: C++ :linenos: :emphasize-lines: 22-26 .. group-tab:: C# .. literalinclude:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/CSharp/main.cs :language: C# :linenos: :emphasize-lines: 23-27 .. group-tab:: Go .. literalinclude:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/Go/main.go :language: go :linenos: :emphasize-lines: 27-31 .. group-tab:: Java .. literalinclude:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/Java/Main.java :language: java :linenos: :emphasize-lines: 26-30 .. group-tab:: JavaScript .. literalinclude:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/JavaScript/main.js :language: javascript :linenos: :emphasize-lines: 14-18 .. group-tab:: Python .. literalinclude:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/Python/main.py :language: python :linenos: :emphasize-lines: 18-21 Example Output ^^^^^^^^^^^^^^ .. image:: ../../code_examples/3-Structured_Problem_Solving/11-Compound_Boolean/vhs.gif :alt: Code example output :align: left