x | y | x OR y | x AND y |
---|---|---|---|
False | False | False | False |
False | True | True | False |
True | False | True | False |
True | True | True | True |
Here is an example of boolean logic. Suppose we have a set of colored shapes. We might ask two kinds of questions. If we choose one of the colored shapes, we might ask: is it blue and a circle? If we choose a blue circle, it is blue and also a circle. If we choose a yellow circle, it is not blue and a circle, because it is yellow (not blue). If we choose a blue square, it is not blue and a circle, because it is a square (not a circle). So whatever shape we choose must satisfy both the requirements, that it be both blue and also a circle.
Now, if we choose one of the colored shapes, we might ask: is it either blue or a circle? If we choose a yellow square, it is obviously not blue, and it is not a circle, so it is not either blue or a circle. But if we choose a blue square, it is either blue or a circle, because it is blue, even though it is not a circle, because we had to satisfy only one of the requirements, that is, that it be either blue or a circle. It need not satisfy both the requirements.
In the same way, if we choose a yellow circle, it satisfies the requirement, because it is a circle, even though it is not blue, because we had to satisfy only one of the requirements.
We can draw a truth table for this specific situation like this:
Color | Shape | Blue OR circle? | Blue AND Circle? |
---|---|---|---|
Blue | Circle | True | True |
Yellow | Circle | True | False |
Blue | Square | True | False |
Yellow | Square | False | False |
We can think about these in relation to more realistic situations. In computer programming, we might need to determine if a number falls within a certain range. This can become confusing because we are mixing numbers, which can have many values, with booleans, which can have only the values True and False.
In the following table, the variable x can take on any numerical value, and 5 is obviously a numerical value, but the expression x>5 can have only two different values: True or False. Either x is greater than 5, or x is not greater than 5. There are no other choices. So we have a numerical expression that uses a comparison operator (> or greater than), and the expression evaluates to a boolean value that is either true or false. This operation that "converts" a wide range of numerical values to the narrow range of boolean values (True and False) is hard to grasp, but for computers, it makes sense and it works.
x | x > 5 | x < 10 | x > 5 OR x < 10 | x > 5 AND x < 10 |
---|---|---|---|---|
7 | True | True | True | True |
4 | False | True | True | False |
11 | True | False | True | False |
No comments:
Post a Comment