Boolean Expressions and Operators

In the last few blocks, we learnt about predicate blocks and conditional blocks. What is one thing common in both of them? Well, predicate blocks evaluate to a boolean value, that is true or false, and conditional blocks use boolean expressions or values to define the code flow.

In this blog, we will deep-dive into the boolean expressions and boolean operators.

What is a Boolean Expression?

Boolean expression is an expression which evaluates to true or false.  The term Boolean is derived from George Boole. As we saw in the previous blogs, all boolean expression blocks in SNAP! can be recognized by their trapezoidal shape.

What are Boolean Operators?

Boolean Operators are the operators used to define the relationship between different terms and evaluate to true or false. There are three Boolean Operators: and, or, and not.

And Operator

And operator evaluates to true only when all the individual expressions involved in the equation are true. If any of the expression is falseand operator will result into false. Let’s take an example of a simple equation with two sub-expressions and see what and operator will evaluate to.

Expression 1 Expression 2 Expression 1 AND Expression 2
false false false
false true false
true false false
true true true

Now it’s not as cryptic as it looks in the first go. Let’s look at this table again with an example of Harry who wants to buy both Chocolates AND Jell Beans from the same store. He goes around the city to different stores, which can fulfill his wish. This is what his examination criteria will look like:

Store Has Chocolate Store Has Jelly Beans Will Harry buy from that store?
Store HAS Chocolate AND Store has jelly beans
false (No) false (No) false (Harry will not buy!)
false (No) true (Yes) false (Harry will not buy!)
true (Yes) false (No) false (Harry will not buy!)
true (Yes) true (Yes) true (Harry will buy!)

Or Operator

Or operator evaluates to false only when all the individual expressions involved in the equation are false. If any of the expression is trueor operator will result into true. Let’s take an example of a simple equation with two sub-expressions and see what or operator will evaluate to.

Expression 1 Expression 2 Expression 1 OR Expression 2
false false false
false true true
true false true
true true true

Again, let’s look at this table with the example of Harry who wants to buy either Chocolates OR Jell Beans from a store. He goes around the city to different stores, which can fulfill his wish. This is what his examination criteria will look like:

Store Has Chocolate Store Has Jelly Beans Will Harry buy from that store?
Store HAS Chocolate OR Store has jelly beans
false (No) false (No) false (Harry will not buy!)
false (No) true (Yes) true (Harry not buy!)
true (Yes) false (No) true (Harry not buy!)
true (Yes) true (Yes) true (Harry will buy!)

Not Operator

Not operator is a simple negation operator. If the input expression is falsenot operator returns true, and if the input expression is truenot operator returns false. Here are the results of applying not operator on any expression:

Expression NOT Expression
false true
true false

Try to use Boolean Operators with some more examples. It may sound confusing at first, but is a very simple concept, which is used often in computer science.

Challenge: What will this Boolean Expression evaluate to: 5 < 7 AND 4 > 2

What does Boolean Operators look like in SNAP!?

SNAP! supports both Boolean expressions and operators. In fact, Boolean operators are also Boolean expressions. You can use the following blocks in Operators palette to use Boolean Operators:

Boolean Operators in SNAP!

Hope this post helped you in enhancing your understanding of Boolean Expressions and Boolean Operators. Feel free to share your feedback/questions in the comments below.

 

 

Share This:

One thought on “Boolean Expressions and Operators

Leave a Reply

Your email address will not be published. Required fields are marked *