8.7 - Boolean Operations
Introduction to Boolean logic
Boolean logic is a fundamental concept in computer science that deals with values that are either true or false. It forms the basis for decision-making in programs, allowing computers to evaluate conditions and make choices based on whether statements are true or false.
Key elements of Boolean logic
- Boolean value - A data type that can only be true or false.
- Boolean expression - A statement that evaluates to a Boolean value, often using comparisons like greater than (>) or equal to (==).
- Boolean operator - A symbol or word that combines Boolean expressions to produce another Boolean value.
The main Boolean operators
There are three primary Boolean operators: AND, OR, and NOT. These operators take Boolean inputs and return a Boolean result (true or false). They are used to build complex conditions by combining simple comparisons.
In some programming languages, these operators have symbolic equivalents:
- AND can be written as &&
- OR can be written as ||
- NOT can be written as !
How the operators work
Each operator has specific rules for when it returns true or false.
AND operator:
- Returns true only if both inputs are true.
- Example: (3 < 5) AND (2 > 1) returns true because both parts are true.
- Example: (4 <= 5) AND (10 > 20) returns false because the second part is false.
OR operator:
- Returns true if at least one input is true.
- Example: (1 > 8) OR (2 == 2) returns true because the second part is true.
- Example: (1 == 8) OR (2 < 2) returns false because both parts are false.
NOT operator:
- Returns the opposite of the input (true becomes false, and false becomes true).
- Example: NOT(5 > 8) returns true because 5 > 8 is false, and NOT reverses it.
- Example: NOT(10 > 6) returns false because 10 > 6 is true, and NOT reverses it.
Truth tables for Boolean operators
AND truth table:
| A | B | A AND B |
|---|---|---|
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
OR truth table:
| A | B | A OR B |
|---|---|---|
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
NOT truth table:
| A | NOT A |
|---|---|
| False | True |
| True | False |
Combining Boolean operators and order of operations
Boolean operators can be combined to create more sophisticated expressions. When combining them, the order in which operations are evaluated matters to ensure the correct result.
Rules for order of operations
Boolean operations follow this precedence:
- Brackets ( ) - Evaluate anything inside brackets first.
- NOT - Apply NOT next.
- AND - Then evaluate AND.
- OR - Finally, evaluate OR.
Always use brackets in complex expressions to make the order clear and avoid errors.
Example of combining operators
Consider the expression: NOT (A OR B) AND C
- First, evaluate inside brackets: A OR B.
- Then apply NOT to that result.
- Finally, AND with C.
Using Boolean operators in selection statements
Selection statements, such as if statements, use Boolean expressions to decide which code to run. Boolean operators make these conditions more flexible by combining multiple checks.
Boolean operators allow a program to test several conditions together.
Example algorithm for character status in a game
In a computer game, a character's survival depends on three variables: hunger, hydration, and comfort. The character dies if:
- Any of the variables are equal to 0.
- Any two of the variables are less than 20.
- All three of the variables are less than 40.
Otherwise, the character stays alive.
Here is pseudocode using Boolean operators in an if-elseif structure:
- The OR checks for any single critical failure.
- The AND combinations test for paired low values.
- Brackets group related conditions to ensure correct evaluation.
Using Boolean operators in iteration statements
Iteration statements, such as loops, use Boolean expressions to decide when to continue or stop repeating. Boolean operators allow loops to run based on multiple conditions, making them more efficient.
Boolean operators enable loops to continue until complex scenarios are met, such as a combination of events.
Example algorithm for scoring in a game
Karen and Stu are playing a 'best out of 10' game. The game ends when one wins 6 rounds or both have won 5 rounds each.
Here is pseudocode using a do-until loop with Boolean operators:
- The loop uses OR to check for a clear winner (6 rounds).
- It includes AND in brackets to detect a 5-5 tie.
- Indentation clarifies the structure, showing the
switchinside the loop.