transcript - New Mexico Computer Science for All

advertisement

Booleans

This presentation will explore the concepts of booleans and boolean logic. We mentioned booleans briefly in the statements and expressions video. In this video we'll have a chance to explore them more fully.

[Slide 1] The most important thing you need to know about booleans, is that they're easy. Why? Well there's only 2 possible answers to a boolean, something that is and it's opposite depending on what you're interested in. So for instance, the 2 possible boolean values could be true or false, or on or off, or for a computer scientist 1 or 0. In this video we'll explore all things boolean using the

2 values of true and false. We will also cover the concepts of boolean expressions, those are expressions that evaluate to a true or false and frequently are used in conditional statements. We will also look at boolean operators which allow us to compare booleans themselves and are used in boolean logic, or boolean math.

[Slide 2] Let's begin with boolean expressions. Boolean expressions are some of the simplest boolean concepts. They use comparison operators such as equal, less than, greater than, less than or equal too, greater than or equal too, and they're used to compare 2 sides to one another, and those 2 sides could be either numbers or variables or expressions themselves, and what a boolean expression does, is it asks a question, it asks the question, " is this expression true or false? " let's take a look at some examples.

Now you've used this comparison operators and math before, for instance 24 +

13 = blank. This is an addition problem and it asks the question, " what does 24 +

13 = ? " and the answer is 37. Now if you put that 37 into the expression and you have and expression that looks like 24 + 13 = 37, that's a boolean expression and it's asking you the question, " 24 + 13 = 37, is that true or false? " and the answer would be true. Another way of saying that, is that it would evaluate to be true.

Let's look at the third expression, " 24 + 13 = 40 is that true or false? " that boolean expression evaluates to be false. Let's look at some more expressions.

Booleans v2

[Slide 3] Here are some easy examples of how comparison operators are used. In the table below, we have boolean expressions on the left and the questions they represent on the right. In this table L represents the left-hand side of the expression and R represent the right-hand side of the expression. In the first example L = R, asks the question, " the left side of the expression equals the right side, is that true or false? " in the case of 3 + 5 = 8, this boolean expression evaluates to the answer true. The next example L is less than R, asks the question, " the left side is less than the right side, true or false? " in the case of

16 ÷ 4 is less than 3, well 16 ÷ 4 is 4, so we have an expression that says basically,

" 4 is less than 3 " and that evaluates to be false. Let's look at the last set of

Page 1 of 4

examples, L is greater than R, this expression asks the question. " the left side is greater than the right side, true or false? " in the case of 50 x 100 is greater than

500, 50 x 100 is 5000 and we know that 5000 is greater than 500 so that would evaluate to be true.

[Slide 4] Let's move on to boolean operators. There are 3 boolean operators

AND, OR, NOT. There are other operators but most of them are based on these three boolean operators. A boolean operator is used to compare 2 boolean values or expressions as shown in the diagram below. This mean that the value or expression on the left side of the operator is a boolean and has a true or false value, and the value or expression on the right side of the boolean operator is also a boolean and it has a true or false value and the result of the expression, using the boolean operator is a new boolean value, another true or false. Each boolean operator has different rules that apply to it and the use of the boolean operators and these rules are what make boolean logic or math.

[Slide 5] Let's move on to boolean logic. Boolean logic actually has a number of different names, it's also known as boolean algebra or boolean math. So what is it? Well we know it's boolean and when something is boolean that means it only has 2 values, in the case of this video, true or false. Boolean logic or boolean math is the math of comparing and manipulating booleans. The interesting thing is that boolean logic was developed in the 1850s by someone called George

Boole, I guess you can figure out where the name boolean comes from. In the

1850s George Boole developed this math in evolving 2 values and it ends up being one of the most important principles of modern computers, without boolean math we wouldn't have computers. So that makes George Boole, someone who lived in the 1850s, one of the founders of computer science. As we already mentioned, boolean operators are used in boolean math and each operator has it's own set of rules and the good news is the rules are relatively easy.

Booleans v2

[Slide 6] Let's look at the boolean operator OR and boolean logic expressions involving the OR operator. For the boolean logic expression A or B to be true, then either A or B must be true. Only one side need to be true for the expression to evaluate to be true. Let's look at the OR truth table below. If both A and B are true when using the boolean operator OR, then the entire expression evaluates to be true. If the A boolean expression is true and the B boolean expression is false, then true or false evaluates to be true. Conversely, if A is false and B is true then the boolean expression A or B is true, and in the last case when both A and

B are false, then the boolean logic expression A or B evaluates to be false. Let's look at out marble again [Slide 7], remember those 2 sets of marbles? When we use the boolean expression, blue is true and red is true, then we ended up with a false because we had no marbles that were both blue and red at that time but when we apply the boolean logic operator, blue is true or red is true, then we

Page 2 of 4

Booleans v2 end up with a very different answer. We get a set that looks like this. The set includes all the marbles, because remember only one side of that boolean logic expression, blue is true of red is true, has to be true for each of those marbles.

Let's look at the set where we have the blue and red marbles. Now in the case of

AND where we had, blue is true and red is true, the result was that small subset of marbles that were both blue and red, but when we are looking for the set where blue is true or red is true, this is what we get. All the marbles meet that criteria, because all of them either have blue or red in them. What about, if we are looking for a set that meets the following boolean logic expression, blue is true or green is true, can we make a set that meets that criteria? And the answer is, yes we can. We can make a set of marbles that meet that criteria because there are marbles that contain blue. What about finding a set in which purple is true or green is true? Well in that case, there is no set that meets that criteria given the set of marbles.

[Slide 8] Let's move on to the last boolean operator, NOT. NOT is really easy to understand because it just gives you the opposite value of whatever you apply it to. So if you apply it to 2 true then it evaluates to false, and if you apply it to false then it evaluates to true. Pretty easy. [Slide 9] Can we find a set with NOT blue equals true? Yes we can, it's the red set. We're looking for the set of marbles

NOT blue is true, which means that they can NOT be blue. Okay, let's find the set of marbles in which NOT red is true. It's the blue set of marbles. Okay, can we find a set that meets the boolean logic expression, NOT purple, being true? All the marbles do that because non of them are purple so you can see that the boolean operator NOT is really very simple. What about that set of marbles where we had the blue and red marbles? Let's find a set of marbles where NOT red is true. It's the ones that just contain blue. How about the set of marbles where NOT blue is true? It would be the marbles that just contain red. What's the set of marbles that have NOT purple being true? That's right, all of the marbles. So you can see that the NOT boolean operator is really very simple.

[Slide 10] So I'm sure you remember when you did simple math, there was a certain order of operation and in boolean logic there's also a certain order of operation and that order of operation is parenthesis, NOT, AND and then OR.

Let's look at some examples. In the expression NOT true and true, we would evaluate the NOT part of the expression first since that falls before the true, in the order of operation. So not true is false. Now we have false and true, well we know, anytime we have one side on an AND statement as false, then that whole expression evaluates to false. Let's look at the next expression. In this case we have parenthesis, so we have NOT and in parenthesis false and true, so based on the order of operations, we would have to evaluate whatever is in the parenthesis first. So false and true would evaluate to be false because one side of the AND statement is false, that would give us NOT false which would

Page 3 of 4

evaluate to true. Here's another example, true OR false AND true, okay well according to the order of operation, you have to evaluate the AND part first, so we would have to evaluate false and true, anytime we have an AND statement with one side of the AND being false then that evaluates to be false and we would get a new boolean logic expression that looks like this. True or false? Now this is an OR expression, so as long as one side of the OR expression is true then the whole thing evaluates to true. One last example, we have parenthesis in this example, in parenthesis we have true or false and then that's used in conjunction with AND true. Parenthesis always go first, so true or false evaluates to be true and that would give us a new boolean expression that looks like, true and true.

Well both sides of that AND statement are true so that would evaluate to be true.

So that's all we're going to talk about, in terms of boolean logic in this video.

[Slide 11] You’re probably wondering why we even discuss booleans, they are frequently used in computer science, the average user can use booleans when they use search engines like Google. You can use boolean logic to limit your search, the boolean order of operation still holds. Now programmers use conditionals and you're going to learn how to use conditionals this week and conditionals use boolean expressions and boolean logic, [Slide 12] but one of the most important way we use booleans is in digital gates or switches which are used to build chips and circuits, without this chips and circuits we wouldn't have computers. So they are the fundamental building block of a computer and they use booleans, but not true and false, they use 1s and 0s, and they use this 1s and

0s to figure out if each gate is open or closed and this is the basic way that a chip works on a board inside your computer.

[Slide 13] In summary, booleans only have 2 values depending on what you're using, true and false, on and off, 1 and 0. Boolean expressions use comparison operators and they evaluate to be true or false. Boolean operators are used to compare boolean values or expressions and to change them. There are 3 basic boolean operators, AND, OR, NOT. Boolean logic or boolean math or boolean algebra, use those boolean operator to compare and manipulate booleans and there is a specific order of operations, parenthesis, NOT, AND and then OR. So that's all we're going to talk about today in terms of booleans. Thank you.

Booleans v2 Page 4 of 4

Download