What are Booleans, and what are they used for?
An assortment of the Boolean blocks that you can find in Scratch.
You may have also heard the term Boolean statement, which refers to a statement that evaluates to either true or false.
This Boolean statement evaluates to true.
“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?
Above are the four Control statements in Scratch that are controlled by Boolean statements.
Boolean blocks fit perfectly within the hexagonal cut-out that certain Control flow statements have.
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.
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?
Let’s say our program has two sprites: Scratch Cat, and a Fish sprite.
After the program is started, 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…”.
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.
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.