By the end of the lesson I will be able to:
The Boolean data type was named for George Boole who defined a formal logic system that used algebra in the mid 19th century. This formal logic system was a way of evaluating statements to either a True or False value, which is exactly what Boolean values are.
We can use Boolean logic to evaluate some simple sentences. For example:
All of the above statements can be evaluated to either True or False, depending on your current situation. Not all sentences are evaluable as True/False, however:
These examples have more complex answers or responses. In computer science we make decisions and change the flow of programs based on the results of True or False evaluations.
In order for us to make decisions in programming we need to evaluate the truth of something. Usually this boils down to some mathematical comparison.
In most programming languages we have 6 basic comparison operators:
DEFINITION: Binary Operator: an operator that needs two inputs to be used. Usually of the form input operator input
Example: 5 < 7, name != "Mr. Rivard"
DEFINITION: Unary Operator: an operator that needs only one input to be used. Usually of the form operator input
Example: not True
While exploring logic, we use truth tables to help us come to logical conclusions. First, we need to find out the truth of our statements, then we can compare these values against one another in more complex ways.
For example if we have the following two statements:
The truth of these statements will change from night to night, but they are definitely evaluable as either True or False. We can represent this more formally like this:
And now we can assign True or False values to the statements.
This allows us to have more complex statements involving operators, just like math. There are three main logical operators:
There is one other important logical operator:
We can represent these relationships in Truth Tables, which are useful tools to refer to when checking the Truth of a complex statement.
The number of possible outcomes is directly tied to the number of input values. You can see from the above that when there is one input value, there are two possible outcomes. When there are 2 input values, there are 4 possible outcomes. The number of possible outcomes is 2n where n is the number of inputs.
So if we have 3 statements joined with connectors (AND, OR, XOR and NOT), we should have 8 possibilities:
(NOT A) AND (B XOR C) Remember: F means False, T means True
Notice that we took this compound, or complex, statement and broke it down into smaller parts. This is done to reduce the number of mistakes made while evaluating the Truth Table.
It can be cumbersome to write AND, OR and NOT all over the place, and programmers are lazy! So a shorthand notation has been created to help shorten the amount that needs to be written. This also aids in Boolean algebra, which is the process of finding equivalent Boolean statements in order to simplify them.
The AND statement is treated similarly to multiplication.
The OR statement is treated similarly to addition
The NOT statement is treated similarly to changing signs (+ to -, or - to +)
The XOR statement is treated similarly to addition, just like OR.
In practice that will mean (NOT A) AND (B XOR C), from the previous example will become:
1. Convert the following longhand Boolean statements into shorthand notation:
2. Convert the following shorthand Boolean statements into longhand notation:
3. Setup and evaluate four truth tables from the previous 2 questions.