How to use booleans in Hopscotch
What is a Boolean, you ask? A boolean has two states, true or false. Although there are no true booleans in Hopscotch, you can use variables set to 1 or 0 to represent the same thing. Today I will show you how to use booleans. Without further ado, let's get started!
First, let’s talk about what booleans do. Since a Boolean is either true or false, they can be used for a certain type of logic. For example, you could have an AI cat that only eats food when it is outside. It would choose a random position – half of the positions are outside, and half are inside. The computer would check to see if the cat is outside or inside. If the cat is outside, it would set a Boolean variable called catOutside to 1 (which represents true). If the cat is inside, then it would set the variable to 0, or false. Then it would check to see if the catOutside variable is 1. If so, it would tell the cat to eat. This is only one of the many uses for Boolean variables!
Now let’s get started.
This is an example of what a program that I made does –
When the answer is true, it shows a 1, and when the information is false, it shows a 0.
Here is a bit of the code behind it -
- Make a line of code that tells the emoji text to pick a random number from 1 to 3. And then tell it that when the number equals 1, make the emoji a certain one, and set the booleans to the correct values. Same for two and three.
The rest of the check once if statements
- For my program, I wanted it to give information on whether the emoji that I was showing was human or dog, happy or sad, and an emoji or not. So I put some text in telling me what each variable meant (the part that says “are you an emoji?” “Are you a dog?” Etc.), and I put a corresponding text object to each question.
For each corresponding text object, I put a set text block inside a forever loop, like this.
- I repeated this process for all of the text objects, changing the variable from “IsSmiling” to “IsCrying”, etc.
- And that was my program. Some other examples of programs using Boolean logic are -
Logic gates and circuits
Explore booleans a bit more, but you don’t need to make anything.
Look at this project –