What are Booleans (in Scratch), and what are they used for?

To give you the short and simple answer, Booleans are “truth values” — they’re a data type that can represent one of two outcomes: true or false. (In some languages, true and false can be represented as 1 or 0, but the takeaway message is the same.) We can also use booleans in Scratch.

In Scratch, Boolean blocks have a very distinctive shape that help you differentiate it from other blocks. Booleans have pointy ends; they look a bit like an elongated hexagon:

An assortment of the Boolean blocks that you can find in Scratch.

Booleans and Boolean statements

You may have also heard the term Boolean statement, which refers to a statement that evaluates to either true or false.

What’s the difference between a Boolean and a Boolean statement? A Boolean simply contains the value true or false, whereas a Boolean statement evaluates to either true or false. To better understand, let’s look at the following example of a Boolean statement:

The above is an example of a Boolean statement because it equates to true.

The sum of 3 and 4 is indeed 7. We can even check this by double-clicking on our Boolean block. A little popup will appear to tell us what information the block contains:

 

This Boolean statement evaluates to true.

Here’s another example of a Boolean statement:

“The sum of 3 and 4 is equal to 10.” This Boolean statement evaluates to false.

This expression is a Boolean statement because it equates to false. We’ve already established that the sum of 3 and 4 is not 10.

What do we use Booleans for?

Booleans are usually associated with control flow statements, which we talked about a few blog posts ago. The most common uses for Booleans are as conditions under which control flow statements are to be executed. What do I mean by this?

Let me draw your attention to a few control flow statements to explain:

Above are the four Control statements in Scratch that are controlled by Boolean statements.

What the above four Control statements have in common is that they are controlled by Booleans. See that elongated hexagonal cut-out in the blocks? That cut-out is where you can put Boolean blocks:

Boolean blocks fit perfectly within the hexagonal cut-out that certain Control flow statements have.

Now, what’s important to recognize is that, for the four Control statements I mentioned, they will only execute if the Boolean statement you provide evaluates to true. So, let’s use the above if-then block as an example:

In this program, Scratch Cat will say “Hello!” because the Boolean statement will evaluate to true.

In this program, Scratch Cat won’t say anything, because the Boolean statement will evaluate to false.

Let’s consider the above two examples. We’ve already established that the Boolean statement 3 + 4 = 7 will evaluate to be true, and the Boolean statement 3 + 4 = 10 will evaluate to be false. In the first example, the if-then block will be executed, because the Boolean statement evaluates to true. In the second example, the if-then block won’t be executed because the Boolean statement evaluates to false.

Try and think of true as a green light and false as a red light. If the Control block is given the green light, it will think, “Oh, I can go”, and it will execute. As long as the Control block is given a red light, however, it will think, “Okay, I need to wait until the light is green before I can do anything.”

The only partial exception to this rule is the if-then-else block, but you won’t need to worry about this block too much for now!

Where can I find Boolean blocks in Scratch?

You’ll find Boolean blocks scattered all over the shop, but the bulk of them can be found in either the Sensing or the Operators category. The reason for this split is because there are two main kinds of Boolean blocks in Scratch:

1. Boolean blocks that check for specific conditions

You can find these types of blocks in the Sensing category. Here’s an example of how to use the touching X block:

Let’s say our program has two sprites: Scratch Cat, and a Fish sprite.

What the program to the right does is: if Scratch Cat touches the fish at any point after the program has been started, then Scratch Cat will say, “Yum!”

2. Boolean blocks that compare values

You can find these types of blocks in the Operators category. Here’s an example of how to use the X < Y operator:

After we start the program, it generates two random numbers. The program checks to see if the first number is smaller than the second number. If the first number is smaller than the second, Scratch Cat will say “Number one is smaller!” If the first number is equal to or larger than the second number, Scratch Cat will say “Number one is not smaller…”.

So, let’s recap everything we’ve learned today:

  • Booleans are “truth values” — they are data type that can contain either the value true or false. (These values may be represented as 1 or 0 in other programming languages!)
  • Boolean statements are statements that evaluate to be true or false. An example of this might be 3 + 4 = 7, because the statement evaluates to true, or 3 + 4 = 10, because the statement evaluates to false.
  • In Scratch, Booleans have a very special shape. They have triangular pointy ends, and they look like elongated hexagons.
  • Boolean statements are generally used to form conditions for control flow statements. You can think of Booleans statements this way:
    • A statement that evaluates to true is like a green light that tells a control flow statement that it can executed.
    • A statement that evaluates to false is like a red light that tells a control flow statement to wait until the colour changes.
  • There are two main types of Boolean statements in Scratch:
    • Boolean blocks that check for specific conditions (you can find these under Sensing)
    • Boolean blocks that compare values (you can find these under Operators)

 

Now you can start using booleans in Scratch.

Happy Scratching!

By Erika Chumpia

 

 

Check out our free teacher download. Learn to code with Scratch in 30 minutes.


If you’re interested to find out more