# Booleans

SparkiDuino
Easy

## 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?

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.) When you first declare a boolean variable if you don’t give it a value it will be automatically set to false. If you want to give the boolean variable a value when you declare it you can do that like this: Now you have a boolean variable named “blerp” which is equal to 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: There is also a really easy way to switch back and forth between the two boolean values. By sticking a ‘!’ onto a variable or an equals sign you are telling the computer to switch the value from whatever it is currently to the opposite value. This way you can turn a true into a false and a false into a true. Think of the ! as something to tell you it’s opposite day for a second and everything is the opposite of what it normally is- a true will turn into false and a false will turn into true. Let’s say you have a variable called “plerb” that you want to set to the opposite value of our first “blop” variable. Here’s what it looks like when someone uses the ! in code: They may seem very simple but it’s important to really understand booleans. After all, they are the foundation for absolutely everything else in computers and robots!

or

## Next Step:

Now that you’ve learned about boolean variables we’ll step it up to variables that have more than two values.