A Simple Yes or No
Booleans are the simplest variable type so we’ll start with them. They are pretty simple and incredibly common. In fact, booleans are the building blocks that make up all computer or robots! Here’s what we’ll be covering on this page:- What’s a Boolean?
- Creating a Boolean
- What Do the Two Values of a Boolean Mean?
- How to Set a Boolean Value
What’s a Boolean?
A boolean is a variable with only two values- True or False. They are useful for keeping track of whether things are happening or not, if motors are on or off and many more things. We have booleans thanks to a guy named George Boole. There are a whole bunch of different ways you can think of booleans- a coin where one side is true and one side is false or maybe a light switch. What other kinds of things have two opposite possibilities like a boolean variable?Creating a Boolean
So how do you make a boolean variable? It’s pretty easy. You need to write the word “bool” so the robot or computer will know that you’re creating a boolean. Then you need to give the variable a name. It can’t have any spaces in it, but coders often make variable with names that are two words smashed together. Pick something that makes sense and you’ll be able to remember later on. It’s no good calling a variable “theVariableThatIMadeSoThatICanControlARobot.” Are you really going to type that over and over again while you’re writing code? It’s also no good calling a variable something like “A3” since you’ll probably have a hard time remembering what the variable does. Here’s an example of creating (also called declaring) a boolean variable named “blap.” (I tend to use silly variable names in these examples.)
1 |
bool blap; |
1 |
bool blerp = true; |
What Do the Two Values of Boolean Mean?
Booleans have two possible values- true and false. These two different values get used in a bunch of different ways, though. When they are used in different ways they will sometimes have different names! (I know, it’s a little confusing but you’ll get used to it.) Here are the different ways you may see boolean values shown: They probably all seem pretty obvious except for the HIGH and LOW values. These values are used when a boolean variable is representing electricity. Often you will use electricity to turn a motor or an LED on or off. When that happens you may used the words HIGH for on and LOW for off. What this really means is that the robot is connecting to all the electricity or as HIGH a voltage as possible for a true value. As you probably guess it also means that the robot is connecting to no electricity or as LOW a voltage as possible for a false value. The other values can be used to answer yes or now questions, turn things on and off and do math. Usually when coders use booleans to do math they will use the one and zero values instead of true or false.Setting Boolean Values
Changing or setting a variable value is called “assigning” a value to the variable. You can think of it kind of like when you are assigned a seat, a locker or a cubby in school. It’s a big deal and you have to remember where your seat is until it is reassigned. In order to assign a boolean value use the following code:
1 2 3 |
blop = true; // or blop = false; |
1 2 3 4 |
blop = true; plerb != blop; // this sets plerb to false // or plerb = !blop; // this also sets plerb to false |